|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# AST1060 Binary Creation and Packaging Script |
| 5 | +# Converts ELF firmware to UART bootable binary format |
| 6 | + |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 9 | + |
| 10 | +usage() { |
| 11 | + echo "Usage: $0 [OPTIONS]" |
| 12 | + echo "" |
| 13 | + echo "OPTIONS:" |
| 14 | + echo " -t, --target TARGET Target triple (default: thumbv7em-none-eabihf)" |
| 15 | + echo " -b, --binary NAME Binary name (default: aspeed-ddk)" |
| 16 | + echo " -o, --output OUTPUT Output binary path (default: target/functional-tests.bin)" |
| 17 | + echo " -s, --max-size SIZE Maximum binary size in bytes (default: 1048576)" |
| 18 | + echo " -h, --help Show this help message" |
| 19 | + echo "" |
| 20 | + echo "EXAMPLES:" |
| 21 | + echo " $0 # Use defaults" |
| 22 | + echo " $0 -b my-firmware -o my-firmware.bin # Custom binary name and output" |
| 23 | + echo " $0 -s 2097152 # Allow 2MB max size" |
| 24 | +} |
| 25 | + |
| 26 | +# Default values |
| 27 | +TARGET="thumbv7em-none-eabihf" |
| 28 | +BINARY_NAME="aspeed-ddk" |
| 29 | +OUTPUT_PATH="target/functional-tests.bin" |
| 30 | +MAX_SIZE=1048576 # 1MB default |
| 31 | + |
| 32 | +# Parse command line arguments |
| 33 | +while [[ $# -gt 0 ]]; do |
| 34 | + case $1 in |
| 35 | + -t|--target) |
| 36 | + TARGET="$2" |
| 37 | + shift 2 |
| 38 | + ;; |
| 39 | + -b|--binary) |
| 40 | + BINARY_NAME="$2" |
| 41 | + shift 2 |
| 42 | + ;; |
| 43 | + -o|--output) |
| 44 | + OUTPUT_PATH="$2" |
| 45 | + shift 2 |
| 46 | + ;; |
| 47 | + -s|--max-size) |
| 48 | + MAX_SIZE="$2" |
| 49 | + shift 2 |
| 50 | + ;; |
| 51 | + -h|--help) |
| 52 | + usage |
| 53 | + exit 0 |
| 54 | + ;; |
| 55 | + *) |
| 56 | + echo "Error: Unknown option $1" |
| 57 | + usage |
| 58 | + exit 1 |
| 59 | + ;; |
| 60 | + esac |
| 61 | +done |
| 62 | + |
| 63 | +# Validate inputs |
| 64 | +if [[ ! "$MAX_SIZE" =~ ^[0-9]+$ ]]; then |
| 65 | + echo "Error: max-size must be a positive integer" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +# Change to project root |
| 70 | +cd "$PROJECT_ROOT" |
| 71 | + |
| 72 | +# Define paths |
| 73 | +ELF_PATH="target/$TARGET/release/$BINARY_NAME" |
| 74 | +RAW_BINARY_PATH="target/functional-tests-raw.bin" |
| 75 | + |
| 76 | +echo "AST1060 Binary Generation" |
| 77 | +echo "=========================" |
| 78 | +echo "Target: $TARGET" |
| 79 | +echo "Binary: $BINARY_NAME" |
| 80 | +echo "Output: $OUTPUT_PATH" |
| 81 | +echo "Max size: $MAX_SIZE bytes" |
| 82 | +echo "" |
| 83 | + |
| 84 | +# Check if ELF file exists |
| 85 | +if [[ ! -f "$ELF_PATH" ]]; then |
| 86 | + echo "Error: ELF file not found at $ELF_PATH" |
| 87 | + echo "Please build the firmware first with:" |
| 88 | + echo " cargo build --release --target $TARGET" |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +# Check for required tools |
| 93 | +if ! command -v arm-none-eabi-objcopy >/dev/null 2>&1; then |
| 94 | + echo "Error: arm-none-eabi-objcopy not found" |
| 95 | + echo "Please install gcc-arm-none-eabi package" |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | + |
| 99 | +echo "Converting ELF to raw binary..." |
| 100 | + |
| 101 | +# Convert ELF to raw binary |
| 102 | +if ! arm-none-eabi-objcopy \ |
| 103 | + -O binary \ |
| 104 | + "$ELF_PATH" \ |
| 105 | + "$RAW_BINARY_PATH"; then |
| 106 | + echo "Error: Failed to convert ELF to binary" |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | + |
| 110 | +# Verify raw binary was created |
| 111 | +if [[ ! -f "$RAW_BINARY_PATH" ]]; then |
| 112 | + echo "Error: Raw binary was not created" |
| 113 | + exit 1 |
| 114 | +fi |
| 115 | + |
| 116 | +# Get raw binary size |
| 117 | +RAW_SIZE=$(stat -c%s "$RAW_BINARY_PATH") |
| 118 | +echo "Raw binary size: $RAW_SIZE bytes" |
| 119 | + |
| 120 | +# Check size limits |
| 121 | +if [[ $RAW_SIZE -gt $MAX_SIZE ]]; then |
| 122 | + echo "Error: Binary too large ($RAW_SIZE bytes > $MAX_SIZE bytes limit)" |
| 123 | + echo "Consider:" |
| 124 | + echo " - Enabling more aggressive optimizations" |
| 125 | + echo " - Reducing feature set" |
| 126 | + echo " - Using release profile with size optimization" |
| 127 | + exit 1 |
| 128 | +fi |
| 129 | + |
| 130 | +echo "Wrapping with UART boot header..." |
| 131 | + |
| 132 | +# Check if UART boot image generator exists |
| 133 | +UART_BOOT_SCRIPT="$SCRIPT_DIR/gen_uart_booting_image.sh" |
| 134 | +if [[ ! -f "$UART_BOOT_SCRIPT" ]]; then |
| 135 | + echo "Error: UART boot image generator not found at $UART_BOOT_SCRIPT" |
| 136 | + echo "Creating simple 4-byte size prefix wrapper..." |
| 137 | + |
| 138 | + # Create simple wrapper if script doesn't exist |
| 139 | + # Write 4-byte little-endian size prefix followed by binary data |
| 140 | + python3 -c " |
| 141 | +import struct |
| 142 | +import sys |
| 143 | +
|
| 144 | +raw_path = '$RAW_BINARY_PATH' |
| 145 | +output_path = '$OUTPUT_PATH' |
| 146 | +max_size = $MAX_SIZE |
| 147 | +
|
| 148 | +try: |
| 149 | + with open(raw_path, 'rb') as f: |
| 150 | + data = f.read() |
| 151 | + |
| 152 | + size = len(data) |
| 153 | + if size > max_size: |
| 154 | + print(f'Error: Binary size {size} exceeds limit {max_size}') |
| 155 | + sys.exit(1) |
| 156 | + |
| 157 | + with open(output_path, 'wb') as f: |
| 158 | + # Write 4-byte little-endian size header |
| 159 | + f.write(struct.pack('<I', size)) |
| 160 | + # Write binary data |
| 161 | + f.write(data) |
| 162 | + |
| 163 | + print(f'Created UART boot image: {output_path}') |
| 164 | + print(f'Header size: 4 bytes') |
| 165 | + print(f'Payload size: {size} bytes') |
| 166 | + print(f'Total size: {size + 4} bytes') |
| 167 | +
|
| 168 | +except Exception as e: |
| 169 | + print(f'Error creating UART boot image: {e}') |
| 170 | + sys.exit(1) |
| 171 | +" |
| 172 | +else |
| 173 | + # Use existing script |
| 174 | + if ! "$UART_BOOT_SCRIPT" "$RAW_BINARY_PATH" "$OUTPUT_PATH"; then |
| 175 | + echo "Error: Failed to create UART boot image" |
| 176 | + exit 1 |
| 177 | + fi |
| 178 | +fi |
| 179 | + |
| 180 | +# Verify final binary was created |
| 181 | +if [[ ! -f "$OUTPUT_PATH" ]]; then |
| 182 | + echo "Error: UART boot image was not created" |
| 183 | + exit 1 |
| 184 | +fi |
| 185 | + |
| 186 | +# Get final sizes |
| 187 | +UART_SIZE=$(stat -c%s "$OUTPUT_PATH") |
| 188 | + |
| 189 | +echo "" |
| 190 | +echo "Binary generation completed successfully!" |
| 191 | +echo "========================================" |
| 192 | +echo "Raw binary: $RAW_BINARY_PATH ($RAW_SIZE bytes)" |
| 193 | +echo "UART image: $OUTPUT_PATH ($UART_SIZE bytes)" |
| 194 | +echo "Header overhead: $((UART_SIZE - RAW_SIZE)) bytes" |
| 195 | + |
| 196 | +# Calculate utilization |
| 197 | +UTILIZATION=$((RAW_SIZE * 100 / MAX_SIZE)) |
| 198 | +echo "Flash utilization: $UTILIZATION% of ${MAX_SIZE} bytes" |
| 199 | + |
| 200 | +if [[ $UTILIZATION -gt 90 ]]; then |
| 201 | + echo "Warning: Flash utilization is high (>90%)" |
| 202 | +fi |
| 203 | + |
| 204 | +echo "" |
| 205 | +echo "Ready for UART upload to AST1060!" |
0 commit comments