|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | +# |
| 4 | +# kuiper2.0 - Embedded Linux for Analog Devices Products |
| 5 | +# |
| 6 | +# Copyright (c) 2024 Analog Devices, Inc. |
| 7 | +# Author: Larisa Radu <[email protected]> |
| 8 | + |
| 9 | +show_help() { |
| 10 | + |
| 11 | +cat << EOF |
| 12 | +Usage: |
| 13 | + sudo $(basename "$0") [OPTIONS] [ARGUMENTS] |
| 14 | +
|
| 15 | +Description: |
| 16 | + Script that prepares Kuiper image to boot on a carrier board. |
| 17 | +
|
| 18 | +Arguments: |
| 19 | + eval-board Name of the project |
| 20 | + carrier Carrier board name |
| 21 | +
|
| 22 | +Options: |
| 23 | + -h, --help Show this help message |
| 24 | +
|
| 25 | +Example: |
| 26 | + sudo $(basename "$0") ad4003 zed |
| 27 | +EOF |
| 28 | + |
| 29 | + echo -e "\n\nAvailable projects in your Kuiper image:\n" |
| 30 | + |
| 31 | + { |
| 32 | + # Print header |
| 33 | + echo -e "ADI Eval Board\tCarrier" |
| 34 | + |
| 35 | + # Print projects and boards |
| 36 | + find /boot -type f -name "*.json" | while read -r file; do |
| 37 | + jq -r ' |
| 38 | + .projects[]? |
| 39 | + | select(has("name") and has("board")) |
| 40 | + | [.name, .board] |
| 41 | + | @tsv |
| 42 | + ' "$file" |
| 43 | + done |
| 44 | + } | column -t -s $'\t' |
| 45 | +} |
| 46 | + |
| 47 | +# Check if the script is run as root |
| 48 | +if [ "$(id -u)" != "0" ] ; then |
| 49 | + echo "This script must be run as root" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +# Handle -h / --help first |
| 54 | +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then |
| 55 | + show_help |
| 56 | + exit 0 |
| 57 | +fi |
| 58 | + |
| 59 | +# Validate positional arguments |
| 60 | +if [[ $# -lt 2 ]]; then |
| 61 | + echo -e "Error: Missing arguments.\n" >&2 |
| 62 | + show_help |
| 63 | + exit 2 |
| 64 | +fi |
| 65 | + |
| 66 | +ADI_EVAL_BOARD=${1} |
| 67 | +CARRIER=${2} |
| 68 | +BOOTLOADER_DEV=${3:-"/dev/mmcblk0p3"} |
| 69 | + |
| 70 | +if [[ ! -z "${ADI_EVAL_BOARD}" && ! -z "${CARRIER}" ]]; then |
| 71 | + |
| 72 | + # Extract project description from .json by selecting the project set in the configuration file |
| 73 | + PROJECT_DESCRIPTION=$(find /boot -type f -name "*.json" \ |
| 74 | + -exec jq -c '.projects[]? | select(.name=="'${ADI_EVAL_BOARD}'" and .board=="'${CARRIER}'")' {} + \ |
| 75 | + | head -n 1) |
| 76 | + |
| 77 | + # Check if project exists |
| 78 | + if [[ -z "${PROJECT_DESCRIPTION}" ]]; then |
| 79 | + echo "Cannot find project ${ADI_EVAL_BOARD} for board ${CARRIER}. Setup not configured." |
| 80 | + else |
| 81 | + # Extract kernel path from the project description |
| 82 | + kernel=$(echo "$PROJECT_DESCRIPTION" | jq -r '.kernel') |
| 83 | + |
| 84 | + # Copy kernel to Kuiper boot directory |
| 85 | + cp -v ${kernel} /boot |
| 86 | + if [ $? -ne 0 ]; then |
| 87 | + echo "Something went wrong while copying the kernel. Boot partition can't be configured." |
| 88 | + exit |
| 89 | + fi |
| 90 | + |
| 91 | + # Extract paths with boot files from the project description |
| 92 | + paths=$(echo "$PROJECT_DESCRIPTION" | jq -r '.files[].path') |
| 93 | + |
| 94 | + # Add the correct prefix for all paths and copy them to Kuiper boot directory |
| 95 | + cp -v $paths /boot |
| 96 | + if [ $? -ne 0 ]; then |
| 97 | + echo "Something went wrong while copying boot files. Boot partition can't be configured." |
| 98 | + exit |
| 99 | + fi |
| 100 | + |
| 101 | + # Check if project platform is Intel |
| 102 | + if [[ ! -z $(echo "$PROJECT_DESCRIPTION" | jq 'select(.platform == "intel")') ]]; then |
| 103 | + |
| 104 | + # Create folder if it doesn't exist and move copied extlinux.conf to extlinux folder |
| 105 | + mkdir -p /boot/extlinux/ && mv -v /boot/extlinux.conf /boot/extlinux/ |
| 106 | + |
| 107 | + # Extract preloader file from the project description |
| 108 | + preloader=$(echo "$PROJECT_DESCRIPTION" | jq -r '.preloader') |
| 109 | + |
| 110 | + # Write preloader file to corresponding image partition |
| 111 | + dd if=${preloader} of=${BOOTLOADER_DEV} status=progress |
| 112 | + if [ $? -ne 0 ]; then |
| 113 | + echo "Something went wrong while copying the preloader. Boot partition can't be configured." |
| 114 | + exit |
| 115 | + fi |
| 116 | + fi |
| 117 | + echo "Successfully prepared boot partition for running project ${ADI_EVAL_BOARD} on ${CARRIER}." |
| 118 | + fi |
| 119 | +else |
| 120 | + echo "Setup won't be configured because setup variables are null." |
| 121 | +fi |
| 122 | + |
0 commit comments