OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 |
| 3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 # This utility writes a firmware image to a tegra based board. |
| 8 |
| 9 # Include common CrOS utilities. |
| 10 . "/usr/lib/crosutils/common.sh" |
| 11 |
| 12 get_default_board |
| 13 |
| 14 # Command line options |
| 15 DEFINE_string board "${DEFAULT_BOARD}" "The name of the board to flash." |
| 16 DEFINE_string variant "" "Board variant." |
| 17 DEFINE_string firmware "" "Path to firmware image to write." |
| 18 DEFINE_string flasher "" "Path to flasher image to use." |
| 19 DEFINE_string bct "" "Path to bct image to write." |
| 20 DEFINE_string map "" "Path to System.map file to read for load address." |
| 21 DEFINE_string flash "" "Boot flash parameter file." |
| 22 DEFINE_string config "" "Directory for temporary configuration files." |
| 23 DEFINE_boolean sign $FLAGS_FALSE "Sign and append a BCT to the firmware image." |
| 24 |
| 25 # Parse command line. |
| 26 FLAGS "$@" || exit 1 |
| 27 eval set -- "${FLAGS_ARGV}" |
| 28 |
| 29 # Die on errors. |
| 30 set -e |
| 31 |
| 32 CROS_LOG_PREFIX="cros_write_firmware" |
| 33 |
| 34 if [ -z "${FLAGS_board}" ]; then |
| 35 error "--board required." |
| 36 exit 1 |
| 37 fi |
| 38 |
| 39 get_board_and_variant $FLAGS_board $FLAGS_variant |
| 40 |
| 41 # Display the U-Boot version string. |
| 42 function show_u_boot_info() { |
| 43 local firmware=$1 |
| 44 local header=$(strings "${firmware}" | grep "U-Boot" | head -1 ) |
| 45 |
| 46 info "Using ${header}" |
| 47 } |
| 48 |
| 49 # Given two values in decimal format return the first value rounded up to a |
| 50 # multiple of the second value. The second value must be a power of two. |
| 51 function round() { |
| 52 awk "BEGIN { print and(($1 + $2 - 1), compl($2 - 1)) }" |
| 53 } |
| 54 |
| 55 # Given a value in decimal format return that value converted to hexidecimal |
| 56 # and formatted as 0xXXXXXXXX. |
| 57 function to_hex() { |
| 58 awk "BEGIN { printf \"0x%x\", $1 }" |
| 59 } |
| 60 |
| 61 # Create the flashing script. This script will be loaded and run by the flasher |
| 62 # build of U-Boot. Most of the script is constant, we just need to append the |
| 63 # sizes and address of the payload. |
| 64 # |
| 65 # TODO(robotboy): Compute the address from the text_base (0xe08000) + |
| 66 # flasher image size (0x40000) + script block (0x1000) |
| 67 function make_flashing_script() { |
| 68 local firmware=$1 |
| 69 local firmware_size=$(stat -c"%s" "${firmware}") |
| 70 |
| 71 echo "setenv address 0xe49000" |
| 72 echo "setenv firmware_size $(to_hex ${firmware_size})" |
| 73 echo "setenv length $(to_hex $(round ${firmware_size} 4096))" |
| 74 |
| 75 cat /usr/share/cros_write_firmware/spi.script |
| 76 } |
| 77 |
| 78 # Parse the System.map file generated by the U-Boot build to get the entry |
| 79 # point address. |
| 80 function get_text_base() { |
| 81 local map=$1 |
| 82 |
| 83 grep -m1 -E "^[0-9a-fA-F]{8} T _start$" ${map} | cut -d " " -f 1 |
| 84 } |
| 85 |
| 86 ############################################################################### |
| 87 |
| 88 u_boot="/build/${BOARD_VARIANT}/u-boot" |
| 89 |
| 90 # Generate default values for the firmware, flasher, BCT, map and flash config |
| 91 # file paths from the board and variant information provided. |
| 92 firmware=${FLAGS_firmware:-"${u_boot}/image.bin"} |
| 93 flasher=${FLAGS_flasher:-"${u_boot}/u-boot-flasher.bin"} |
| 94 bct=${FLAGS_bct:-"${u_boot}/board.bct"} |
| 95 map=${FLAGS_map:-"${u_boot}/System.map"} |
| 96 flash=${FLAGS_flash:-"${u_boot}/flash.cfg"} |
| 97 |
| 98 # Extract the load and entry point for U-Boot. |
| 99 text_base="0x$(get_text_base "${map}")" |
| 100 |
| 101 # Verify that all of the files and tools we will need are present. |
| 102 check_for_file "firmware" " " "${firmware}" |
| 103 check_for_file "flasher" " " "${flasher}" |
| 104 check_for_file "BCT" " " "${bct}" |
| 105 check_for_file "map" " " "${map}" |
| 106 check_for_file "flash" " " "${flash}" |
| 107 |
| 108 check_for_tool "nvflash" "nvflash" |
| 109 |
| 110 show_u_boot_info "${firmware}" |
| 111 |
| 112 # If the user has not specified a config directory then create one on the fly |
| 113 # and remove it when we are done. |
| 114 if [ -z "${FLAGS_config}" ]; then |
| 115 config=$(mktemp -d) |
| 116 |
| 117 trap "rm -rf ${config}" EXIT |
| 118 else |
| 119 mkdir -p "${FLAGS_config}" |
| 120 |
| 121 config=${FLAGS_config} |
| 122 fi |
| 123 |
| 124 # Optionally sign the firmware. This is usefull if you want to write a bare |
| 125 # U-Boot to SPI and boot with that instead of a fully build ChromiumOS firmware |
| 126 # image. |
| 127 if [ $FLAGS_sign -eq $FLAGS_TRUE ]; then |
| 128 signed_firmware=${config}/signed.bin |
| 129 |
| 130 cros_sign_bootstub \ |
| 131 --bct "${bct}" \ |
| 132 --flash "${flash}" \ |
| 133 --bootstub "${firmware}" \ |
| 134 --output "${signed_firmware}" \ |
| 135 --config "${config}" \ |
| 136 --text_base "${text_base}" |
| 137 else |
| 138 signed_firmware=${firmware} |
| 139 fi |
| 140 |
| 141 # Create the flashing script and turn it into a U-Boot uImage. |
| 142 make_flashing_script "${signed_firmware}" > ${config}/script |
| 143 |
| 144 mkimage -A arm -T script -d ${config}/script ${config}/script.uimg > /dev/null |
| 145 |
| 146 # Construct RAM image of flasher + script + firmware. The firmware should |
| 147 # already be signed by cbootimage. The RAM image has a fixed 256K partition |
| 148 # for the flasher, followed by the script and payload to be written. |
| 149 dd if="${flasher}" of="${config}/image" &> /dev/null |
| 150 dd if="${config}/script.uimg" of="${config}/image" seek=512 &> /dev/null |
| 151 dd if="${signed_firmware}" of="${config}/image" seek=520 &> /dev/null |
| 152 |
| 153 # Write the RAM image image to the board. The flasher will write the payload |
| 154 # into the boot device. The warning below about the failure is due to the |
| 155 # way that we are using nvflash. It expects to be able to talk to the flashing |
| 156 # bootloader that is passed down, but U-Boot doesn't talk back the way |
| 157 # bootloader.bin did, and nvflash complains. |
| 158 sudo nvflash \ |
| 159 --configfile /usr/share/cros_write_firmware/nvflash-spi.cfg \ |
| 160 --bct "${bct}" \ |
| 161 --setbct \ |
| 162 --bl "${config}/image" \ |
| 163 --go \ |
| 164 --setentry "${text_base}" "${text_base}" || |
| 165 warn "The above 'command failure: go failed' can be ignored." |
| 166 |
| 167 # TODO(robotboy): Make a U-Boot tool that can compute the same checksum on |
| 168 # the host and display it here. Unfortunately U-Boot uses a slightly different |
| 169 # crc32 than zlib, so it's not possible to use zlib's crc32 to do this. It has |
| 170 # to be done by building a tool that uses U-Boots version. |
| 171 info "The serial terminal or LCD panel should show before and after CRCs of" |
| 172 info "the image written to the boot flash. If these values match each other" |
| 173 info "your flash was probably successful." |
OLD | NEW |