blob: 44456d32956cabf1314b67102889875d688e672a [file] [log] [blame]
qs.xiongdf2e8c02025-07-02 16:50:13 +08001#!/bin/bash
2# ASR1806 Fast Partition Configuration Generator
3# Usage: ./config_fast_partion_list.sh --dtsi <dtsi> --config <json> --output <header>
4
5set -eo pipefail
6
7# ---------------------------
8# 彩色输出定义
9# ---------------------------
10RED='\033[0;31m'
11GREEN='\033[0;32m'
12YELLOW='\033[1;33m'
13NC='\033[0m' # No Color
14
15# ---------------------------
16# 日志函数
17# ---------------------------
18timestamp() {
19 date "+%Y-%m-%d %H:%M:%S"
20}
21
22log() {
23 echo -e "[$(timestamp)] $1"
24}
25
26log_success() {
27 echo -e "[$(timestamp)] ${GREEN}SUCCESS${NC} $1"
28}
29
30log_warning() {
31 echo -e "[$(timestamp)] ${YELLOW}WARNING${NC} $1"
32}
33
34log_error() {
35 echo -e "[$(timestamp)] ${RED}ERROR${NC} $1" >&2
36}
37
38# ---------------------------
39# 参数处理
40# ---------------------------
41show_help() {
42 echo -e "${GREEN}Usage:${NC} $0 [options]"
43 echo
44 echo -e "${YELLOW}Mandatory Options:${NC}"
45 echo " -d, --dtsi Path to input DTSI partition file"
46 echo " -c, --config Path to JSON configuration file"
47 echo " -o, --output Path to output header file"
48 echo
49 echo -e "${YELLOW}Optional Options:${NC}"
50 echo " -v, --verbose Enable verbose output"
51 echo " -h, --help Show this help message"
52 exit 0
53}
54
55# 解析参数
56while [[ $# -gt 0 ]]; do
57 case "$1" in
58 -d|--dtsi)
59 DTSI_FILE="$2"
60 shift 2
61 ;;
62 -c|--config)
63 CONFIG_FILE="$2"
64 shift 2
65 ;;
66 -o|--output)
67 HEADER_FILE="$2"
68 shift 2
69 ;;
70 -v|--verbose)
71 VERBOSE=true
72 shift
73 ;;
74 -h|--help)
75 show_help
76 ;;
77 *)
78 log_error "Unknown option: $1"
79 show_help
80 exit 1
81 ;;
82 esac
83done
84
85# ---------------------------
86# 环境检查
87# ---------------------------
88check_requirements() {
89 log "Checking system requirements..."
90
91 # 检查必需参数
92 local missing=()
93 [[ -z "$DTSI_FILE" ]] && missing+=("--dtsi")
94 [[ -z "$CONFIG_FILE" ]] && missing+=("--config")
95 [[ -z "$HEADER_FILE" ]] && missing+=("--output")
96
97 if [[ ${#missing[@]} -gt 0 ]]; then
98 log_error "Missing required options: ${missing[*]}"
99 show_help
100 exit 1
101 fi
102
103 # 检查依赖
104 if ! command -v python3 &>/dev/null; then
105 log_error "python3 is required but not installed"
106 exit 1
107 fi
108
109 log_success "All requirements satisfied"
110}
111
112validate_paths() {
113 log "Validating input paths..."
114
115 # 检查输入文件
116 for file in "$DTSI_FILE" "$CONFIG_FILE"; do
117 if [[ ! -f "$file" ]]; then
118 log_error "File not found: $file"
119 exit 1
120 fi
121 log "Found: $file"
122 done
123
124 # 准备输出目录
125 mkdir -p "$(dirname "$HEADER_FILE")" || {
126 log_error "Failed to create output directory: $(dirname "$HEADER_FILE")"
127 exit 1
128 }
129 log "Output will be written to: $HEADER_FILE"
130
131 log_success "Path validation passed"
132}
133
134# ---------------------------
135# 主逻辑
136# ---------------------------
137generate_partitions() {
138 local py_script="$(dirname "$0")/generate_partitions.py"
139
140 log "Starting partition configuration generation..."
141 log "--------------------------------------------"
142 log "Input DTSI: $DTSI_FILE"
143 log "Config JSON: $CONFIG_FILE"
144 log "Output Header: $HEADER_FILE"
145 log "Python Script: $py_script"
146 log "--------------------------------------------"
147
148 local py_cmd=(python3 "$py_script"
149 --dtsi "$DTSI_FILE"
150 --config "$CONFIG_FILE"
151 --header "$HEADER_FILE"
152 )
153
154 [[ $VERBOSE == true ]] && py_cmd+=(--verbose)
155
156 if ! "${py_cmd[@]}"; then
157 log_error "Partition generation failed"
158 exit 1
159 fi
160
161 log_success "Configuration generated successfully"
162}
163
164verify_output() {
165 log "Verifying output file..."
166
167 if [[ ! -f "$HEADER_FILE" ]]; then
168 log_error "Output file was not created: $HEADER_FILE"
169 exit 1
170 fi
171
172 local line_count=$(wc -l < "$HEADER_FILE")
173 local generated_lines=$(grep -c "Auto-generated partition config" "$HEADER_FILE")
174
175 log "Output file info:"
176 log " Path: $HEADER_FILE"
177 log " Lines: $line_count"
178 log " Generated: $generated_lines"
179
180 if [[ $generated_lines -eq 0 ]]; then
181 log_warning "No generated content found in output file"
182 fi
183
184 log_success "Output verification passed"
185}
186
187# ---------------------------
188# 主流程
189# ---------------------------
190main() {
191 echo -e "\n${GREEN}=== ASR1806 Partition Config Generator ===${NC}"
192
193 check_requirements
194 validate_paths
195 generate_partitions
196 verify_output
197
198 echo -e "\n${GREEN}=== Operation Completed Successfully ===${NC}"
199 echo -e "Output file created: ${YELLOW}$HEADER_FILE${NC}"
200}
201
202main