| #!/bin/bash | 
 | # ASR1806 Fast Partition Configuration Generator | 
 | # Usage: ./config_fast_partion_list.sh --dtsi <dtsi> --config <json> --output <header> | 
 |  | 
 | set -eo pipefail | 
 |  | 
 | # --------------------------- | 
 | # 彩色输出定义 | 
 | # --------------------------- | 
 | RED='\033[0;31m' | 
 | GREEN='\033[0;32m' | 
 | YELLOW='\033[1;33m' | 
 | NC='\033[0m' # No Color | 
 |  | 
 | # --------------------------- | 
 | # 日志函数 | 
 | # --------------------------- | 
 | timestamp() { | 
 |     date "+%Y-%m-%d %H:%M:%S" | 
 | } | 
 |  | 
 | log() { | 
 |     echo -e "[$(timestamp)] $1" | 
 | } | 
 |  | 
 | log_success() { | 
 |     echo -e "[$(timestamp)] ${GREEN}SUCCESS${NC} $1" | 
 | } | 
 |  | 
 | log_warning() { | 
 |     echo -e "[$(timestamp)] ${YELLOW}WARNING${NC} $1" | 
 | } | 
 |  | 
 | log_error() { | 
 |     echo -e "[$(timestamp)] ${RED}ERROR${NC} $1" >&2 | 
 | } | 
 |  | 
 | # --------------------------- | 
 | # 参数处理 | 
 | # --------------------------- | 
 | show_help() { | 
 |     echo -e "${GREEN}Usage:${NC} $0 [options]" | 
 |     echo | 
 |     echo -e "${YELLOW}Mandatory Options:${NC}" | 
 |     echo "  -d, --dtsi     Path to input DTSI partition file" | 
 |     echo "  -c, --config   Path to JSON configuration file" | 
 |     echo "  -o, --output   Path to output header file" | 
 |     echo | 
 |     echo -e "${YELLOW}Optional Options:${NC}" | 
 |     echo "  -v, --verbose  Enable verbose output" | 
 |     echo "  -h, --help     Show this help message" | 
 |     exit 0 | 
 | } | 
 |  | 
 | # 解析参数 | 
 | while [[ $# -gt 0 ]]; do | 
 |     case "$1" in | 
 |         -d|--dtsi) | 
 |             DTSI_FILE="$2" | 
 |             shift 2 | 
 |             ;; | 
 |         -c|--config) | 
 |             CONFIG_FILE="$2" | 
 |             shift 2 | 
 |             ;; | 
 |         -o|--output) | 
 |             HEADER_FILE="$2" | 
 |             shift 2 | 
 |             ;; | 
 |         -v|--verbose) | 
 |             VERBOSE=true | 
 |             shift | 
 |             ;; | 
 |         -h|--help) | 
 |             show_help | 
 |             ;; | 
 |         *) | 
 |             log_error "Unknown option: $1" | 
 |             show_help | 
 |             exit 1 | 
 |             ;; | 
 |     esac | 
 | done | 
 |  | 
 | # --------------------------- | 
 | # 环境检查 | 
 | # --------------------------- | 
 | check_requirements() { | 
 |     log "Checking system requirements..." | 
 |      | 
 |     # 检查必需参数 | 
 |     local missing=() | 
 |     [[ -z "$DTSI_FILE" ]] && missing+=("--dtsi") | 
 |     [[ -z "$CONFIG_FILE" ]] && missing+=("--config") | 
 |     [[ -z "$HEADER_FILE" ]] && missing+=("--output") | 
 |  | 
 |     if [[ ${#missing[@]} -gt 0 ]]; then | 
 |         log_error "Missing required options: ${missing[*]}" | 
 |         show_help | 
 |         exit 1 | 
 |     fi | 
 |  | 
 |     # 检查依赖 | 
 |     if ! command -v python3 &>/dev/null; then | 
 |         log_error "python3 is required but not installed" | 
 |         exit 1 | 
 |     fi | 
 |  | 
 |     log_success "All requirements satisfied" | 
 | } | 
 |  | 
 | validate_paths() { | 
 |     log "Validating input paths..." | 
 |      | 
 |     # 检查输入文件 | 
 |     for file in "$DTSI_FILE" "$CONFIG_FILE"; do | 
 |         if [[ ! -f "$file" ]]; then | 
 |             log_error "File not found: $file" | 
 |             exit 1 | 
 |         fi | 
 |         log "Found: $file" | 
 |     done | 
 |  | 
 |     # 准备输出目录 | 
 |     mkdir -p "$(dirname "$HEADER_FILE")" || { | 
 |         log_error "Failed to create output directory: $(dirname "$HEADER_FILE")" | 
 |         exit 1 | 
 |     } | 
 |     log "Output will be written to: $HEADER_FILE" | 
 |  | 
 |     log_success "Path validation passed" | 
 | } | 
 |  | 
 | # --------------------------- | 
 | # 主逻辑 | 
 | # --------------------------- | 
 | generate_partitions() { | 
 |     local py_script="$(dirname "$0")/generate_partitions.py" | 
 |      | 
 |     log "Starting partition configuration generation..." | 
 |     log "--------------------------------------------" | 
 |     log "Input DTSI:    $DTSI_FILE" | 
 |     log "Config JSON:   $CONFIG_FILE" | 
 |     log "Output Header: $HEADER_FILE" | 
 |     log "Python Script: $py_script" | 
 |     log "--------------------------------------------" | 
 |  | 
 |     local py_cmd=(python3 "$py_script" | 
 |         --dtsi "$DTSI_FILE" | 
 |         --config "$CONFIG_FILE" | 
 |         --header "$HEADER_FILE" | 
 |     ) | 
 |      | 
 |     [[ $VERBOSE == true ]] && py_cmd+=(--verbose) | 
 |  | 
 |     if ! "${py_cmd[@]}"; then | 
 |         log_error "Partition generation failed" | 
 |         exit 1 | 
 |     fi | 
 |  | 
 |     log_success "Configuration generated successfully" | 
 | } | 
 |  | 
 | verify_output() { | 
 |     log "Verifying output file..." | 
 |      | 
 |     if [[ ! -f "$HEADER_FILE" ]]; then | 
 |         log_error "Output file was not created: $HEADER_FILE" | 
 |         exit 1 | 
 |     fi | 
 |  | 
 |     local line_count=$(wc -l < "$HEADER_FILE") | 
 |     local generated_lines=$(grep -c "Auto-generated partition config" "$HEADER_FILE") | 
 |      | 
 |     log "Output file info:" | 
 |     log "  Path:       $HEADER_FILE" | 
 |     log "  Lines:      $line_count" | 
 |     log "  Generated:  $generated_lines" | 
 |      | 
 |     if [[ $generated_lines -eq 0 ]]; then | 
 |         log_warning "No generated content found in output file" | 
 |     fi | 
 |  | 
 |     log_success "Output verification passed" | 
 | } | 
 |  | 
 | # --------------------------- | 
 | # 主流程 | 
 | # --------------------------- | 
 | main() { | 
 |     echo -e "\n${GREEN}=== ASR1806 Partition Config Generator ===${NC}" | 
 |      | 
 |     check_requirements | 
 |     validate_paths | 
 |     generate_partitions | 
 |     verify_output | 
 |      | 
 |     echo -e "\n${GREEN}=== Operation Completed Successfully ===${NC}" | 
 |     echo -e "Output file created: ${YELLOW}$HEADER_FILE${NC}" | 
 | } | 
 |  | 
 | main |