#!/bin/bash ############################################################################## # Configuration Switching Script for demo_ld NOR/NAND Dual Configuration # # Usage: ./switch-config.sh [nor|nand] # # This script manages partition configuration files by copying: # - partition-config-nor.json | partition-config-nand.json -> partition-config.json # - partition-info-nor.txt | partition-info-nand.txt -> partition-info.txt # # Default configuration: NAND ############################################################################## set -e # Script directory (product/demo_ld) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Root directory ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" # Configuration file paths CONFIG_JSON_NOR="partition-config-nor.json" CONFIG_JSON_NAND="partition-config-nand.json" CONFIG_JSON_LINK="partition-config.json" INFO_TXT_NOR="partition-info-nor.txt" INFO_TXT_NAND="partition-info-nand.txt" INFO_TXT_LINK="partition-info.txt" # Defconfig file paths (in product/demo_ld directory) DEFCONFIG_NOR="project_demo_ld_nor_defconfig" DEFCONFIG_NAND="project_demo_ld_nand_defconfig" DEFCONFIG_LINK="project_demo_ld_defconfig" # Color codes for output COLOR_GREEN='\033[0;32m' COLOR_YELLOW='\033[1;33m' COLOR_RED='\033[0;31m' COLOR_BLUE='\033[0;34m' COLOR_RESET='\033[0m' ############################################################################## # Function: print_usage # Description: Display usage information ############################################################################## print_usage() { echo "Usage: $0 [nor|nand]" echo "" echo "Arguments:" echo " nor - Switch to NOR flash configuration (16MB)" echo " nand - Switch to NAND flash configuration (128MB)" echo "" echo "Examples:" echo " $0 nor # Switch to NOR configuration" echo " $0 nand # Switch to NAND configuration" } ############################################################################## # Function: print_colored # Description: Print colored message # Arguments: $1 = color code, $2 = message ############################################################################## print_colored() { echo -e "${1}${2}${COLOR_RESET}" } ############################################################################## # Function: get_current_config # Description: Detect current configuration by comparing file content # Returns: "nor", "nand", or "unknown" ############################################################################## get_current_config() { if [ -f "$SCRIPT_DIR/$CONFIG_JSON_LINK" ]; then # Compare with NOR config if diff -q "$SCRIPT_DIR/$CONFIG_JSON_LINK" "$SCRIPT_DIR/$CONFIG_JSON_NOR" > /dev/null 2>&1; then echo "nor" return fi # Compare with NAND config if diff -q "$SCRIPT_DIR/$CONFIG_JSON_LINK" "$SCRIPT_DIR/$CONFIG_JSON_NAND" > /dev/null 2>&1; then echo "nand" return fi echo "unknown" else echo "none" fi } ############################################################################## # Function: display_current_config # Description: Display current configuration status ############################################################################## display_current_config() { local current=$(get_current_config) print_colored "$COLOR_BLUE" "==========================================" print_colored "$COLOR_BLUE" " Current Configuration Status" print_colored "$COLOR_BLUE" "==========================================" if [ "$current" == "nor" ]; then print_colored "$COLOR_GREEN" "Current Config: NOR Flash (16MB)" echo " - partition-config.json -> $CONFIG_JSON_NOR" echo " - partition-info.txt -> $INFO_TXT_NOR" echo " - Storage Type: SPI NOR" echo " - Filesystem: jffs2/squashfs" echo " - Partitions: 8 (no system, recovery-rootfs, usrdata)" elif [ "$current" == "nand" ]; then print_colored "$COLOR_GREEN" "Current Config: NAND Flash (128MB)" echo " - partition-config.json -> $CONFIG_JSON_NAND" echo " - partition-info.txt -> $INFO_TXT_NAND" echo " - Storage Type: SPI NAND" echo " - Filesystem: ubi" echo " - Partitions: 14 (includes system, recovery-rootfs, usrdata)" elif [ "$current" == "none" ]; then print_colored "$COLOR_YELLOW" "Current Config: No soft links found" print_colored "$COLOR_YELLOW" "WARNING: Configuration not initialized" else print_colored "$COLOR_RED" "Current Config: Unknown/Invalid" print_colored "$COLOR_RED" "ERROR: Soft link points to unexpected target" fi print_colored "$COLOR_BLUE" "==========================================" echo "" } ############################################################################## # Function: verify_source_files # Description: Verify that all required source files exist # Returns: 0 if all files exist, 1 otherwise ############################################################################## verify_source_files() { local all_exist=0 print_colored "$COLOR_BLUE" "Verifying source files..." for file in "$CONFIG_JSON_NOR" "$CONFIG_JSON_NAND" "$INFO_TXT_NOR" "$INFO_TXT_NAND"; do if [ ! -f "$SCRIPT_DIR/$file" ]; then print_colored "$COLOR_RED" "ERROR: Missing file: $file" all_exist=1 else echo " [OK] $file" fi done # Verify defconfig files for file in "$DEFCONFIG_NOR" "$DEFCONFIG_NAND"; do if [ ! -f "$SCRIPT_DIR/$file" ]; then print_colored "$COLOR_RED" "ERROR: Missing defconfig file: $file" all_exist=1 else echo " [OK] $file" fi done echo "" return $all_exist } ############################################################################## # Function: copy_config_files # Description: Copy configuration files for specified configuration # Arguments: $1 = "nor" or "nand" ############################################################################## copy_config_files() { local config_type="$1" local json_source="" local txt_source="" local defconfig_source="" if [ "$config_type" == "nor" ]; then json_source="$CONFIG_JSON_NOR" txt_source="$INFO_TXT_NOR" defconfig_source="$DEFCONFIG_NOR" elif [ "$config_type" == "nand" ]; then json_source="$CONFIG_JSON_NAND" txt_source="$INFO_TXT_NAND" defconfig_source="$DEFCONFIG_NAND" else print_colored "$COLOR_RED" "ERROR: Invalid configuration type: $config_type" return 1 fi print_colored "$COLOR_BLUE" "Copying configuration files for $config_type configuration..." # Backup existing files if they exist if [ -f "$SCRIPT_DIR/$CONFIG_JSON_LINK" ]; then cp "$SCRIPT_DIR/$CONFIG_JSON_LINK" "$SCRIPT_DIR/$CONFIG_JSON_LINK.backup" echo " [OK] Backed up: $CONFIG_JSON_LINK -> $CONFIG_JSON_LINK.backup" fi if [ -f "$SCRIPT_DIR/$INFO_TXT_LINK" ]; then cp "$SCRIPT_DIR/$INFO_TXT_LINK" "$SCRIPT_DIR/$INFO_TXT_LINK.backup" echo " [OK] Backed up: $INFO_TXT_LINK -> $INFO_TXT_LINK.backup" fi if [ -f "$SCRIPT_DIR/$DEFCONFIG_LINK" ]; then cp "$SCRIPT_DIR/$DEFCONFIG_LINK" "$SCRIPT_DIR/$DEFCONFIG_LINK.backup" echo " [OK] Backed up: $DEFCONFIG_LINK -> $DEFCONFIG_LINK.backup" fi # Copy new configuration files cp "$SCRIPT_DIR/$json_source" "$SCRIPT_DIR/$CONFIG_JSON_LINK" cp "$SCRIPT_DIR/$txt_source" "$SCRIPT_DIR/$INFO_TXT_LINK" cp "$SCRIPT_DIR/$defconfig_source" "$SCRIPT_DIR/$DEFCONFIG_LINK" echo " [OK] Copied: $json_source -> $CONFIG_JSON_LINK" echo " [OK] Copied: $txt_source -> $INFO_TXT_LINK" echo " [OK] Copied: $defconfig_source -> $DEFCONFIG_LINK" echo "" return 0 } ############################################################################## # Function: display_switch_result # Description: Display result after switching configuration # Arguments: $1 = target configuration ("nor" or "nand") ############################################################################## display_switch_result() { local target="$1" print_colored "$COLOR_GREEN" "==========================================" print_colored "$COLOR_GREEN" " Configuration Switched Successfully" print_colored "$COLOR_GREEN" "==========================================" if [ "$target" == "nor" ]; then print_colored "$COLOR_GREEN" "New Config: NOR Flash (16MB)" echo "" echo "Files updated:" echo " - partition-config.json (-> partition-config-nor.json)" echo " - partition-info.txt (-> partition-info-nor.txt)" echo " - project_demo_ld_defconfig (-> project_demo_ld_nor_defconfig)" echo "" echo "Next Steps:" echo " 1. Build the project:" echo " $ make project_demo_ld_defconfig && make xos -j8" echo "" echo " 2. Flash images will be generated in:" echo " out/qm10xd_linux/qmimages/" echo "" echo " 3. Expected output files:" echo " - u-boot-spl-header.img" echo " - u-boot.bin" echo " - zImage-dtb" echo " - rootfs.squashfs" echo " - data.jffs2" echo " - logo.img" echo " - script.ini (for flashing)" elif [ "$target" == "nand" ]; then print_colored "$COLOR_GREEN" "New Config: NAND Flash (128MB)" echo "" echo "Files updated:" echo " - partition-config.json (-> partition-config-nand.json)" echo " - partition-info.txt (-> partition-info-nand.txt)" echo " - project_demo_ld_defconfig (-> project_demo_ld_nand_defconfig)" echo "" echo "Next Steps:" echo " 1. Build the project:" echo " $ make project_demo_ld_defconfig && make xos -j8" echo "" echo " 2. Flash images will be generated in:" echo " out/qm10xd_linux/qmimages/" echo "" echo " 3. Expected output files:" echo " - u-boot-spl-header.img" echo " - u-boot.bin" echo " - zImage-dtb" echo " - system.ubi" echo " - rootfs.ubi" echo " - data.ubi" echo " - usrdata.ubi" echo " - recovery-rootfs.ubi" echo " - logo.img" echo " - script.ini (for flashing)" echo " - programmer/ (for production tool)" fi print_colored "$COLOR_GREEN" "==========================================" echo "" } ############################################################################## # MAIN SCRIPT EXECUTION ############################################################################## # Display current configuration display_current_config # Check arguments if [ $# -eq 0 ]; then print_colored "$COLOR_YELLOW" "No argument provided. Displaying current configuration only." echo "" print_usage exit 0 fi if [ $# -ne 1 ]; then print_colored "$COLOR_RED" "ERROR: Invalid number of arguments" echo "" print_usage exit 1 fi # Validate argument target_config="$1" if [ "$target_config" != "nor" ] && [ "$target_config" != "nand" ]; then print_colored "$COLOR_RED" "ERROR: Invalid argument: $target_config" echo "" print_usage exit 1 fi # Check if already using target configuration current_config=$(get_current_config) if [ "$current_config" == "$target_config" ]; then print_colored "$COLOR_YELLOW" "INFO: Already using $target_config configuration. No changes needed." exit 0 fi # Verify all source files exist if ! verify_source_files; then print_colored "$COLOR_RED" "ERROR: Cannot proceed due to missing files" exit 1 fi # Copy configuration files if ! copy_config_files "$target_config"; then print_colored "$COLOR_RED" "ERROR: Failed to copy configuration files" exit 1 fi # Verify switch was successful new_config=$(get_current_config) if [ "$new_config" != "$target_config" ]; then print_colored "$COLOR_RED" "ERROR: Configuration switch failed (verification mismatch)" exit 1 fi # Display success result display_switch_result "$target_config" exit 0