Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dcuc testing #13

Merged
merged 14 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

workflow_dispatch:

jobs:
Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: build
uses: addnab/docker-run-action@v3
with:
image: dannyben/bashly
options: --rm -v ${{ github.workspace }}:/app
run: bashly generate
- name: tests
uses: addnab/docker-run-action@v3
with:
image: ghcr.io/dasharo/dasharo-sdk:v1.5.0
options: --rm -v ${{ github.workspace }}:/dcu
run: |
cd dcu
test/approve-ci

2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ repos:
- id: shellcheck
args: ["--severity=warning"]
exclude: "^src/.*$"
files: |
^test/approvals/
111 changes: 43 additions & 68 deletions dcu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# This script was generated by bashly 1.1.10 (https://bashly.dannyb.co)
# This script was generated by bashly 1.2.1 (https://bashly.dannyb.co)
# Modifying it manually is not recommended

# :wrapper.bash3_bouncer
Expand All @@ -17,15 +17,7 @@ version_command() {

# :command.usage
dcu_usage() {
if [[ -n $long_usage ]]; then
printf "dcu - Dasharo Configuration Utility\n"
echo

else
printf "dcu - Dasharo Configuration Utility\n"
echo

fi
printf "dcu - Dasharo Configuration Utility\n\n"

printf "%s\n" "Usage:"
printf " dcu COMMAND\n"
Expand Down Expand Up @@ -65,16 +57,7 @@ dcu_usage() {

# :command.usage
dcu_mac_usage() {
if [[ -n $long_usage ]]; then
printf "dcu mac - Get/set MAC address in a firmware image\n"
echo

else
printf "dcu mac - Get/set MAC address in a firmware image\n"
echo

fi

printf "dcu mac - Get/set MAC address in a firmware image\n\n"
printf "Alias: m\n"
echo

Expand Down Expand Up @@ -117,16 +100,7 @@ dcu_mac_usage() {

# :command.usage
dcu_smbios_usage() {
if [[ -n $long_usage ]]; then
printf "dcu smbios - Edit SMBIOS data in a firmware image\n"
echo

else
printf "dcu smbios - Edit SMBIOS data in a firmware image\n"
echo

fi

printf "dcu smbios - Edit SMBIOS data in a firmware image\n\n"
printf "Alias: s\n"
echo

Expand Down Expand Up @@ -175,16 +149,7 @@ dcu_smbios_usage() {

# :command.usage
dcu_logo_usage() {
if [[ -n $long_usage ]]; then
printf "dcu logo - Insert custom logo boot splash into firmware image\n"
echo

else
printf "dcu logo - Insert custom logo boot splash into firmware image\n"
echo

fi

printf "dcu logo - Insert custom logo boot splash into firmware image\n\n"
printf "Alias: l\n"
echo

Expand Down Expand Up @@ -226,16 +191,7 @@ dcu_logo_usage() {

# :command.usage
dcu_variable_usage() {
if [[ -n $long_usage ]]; then
printf "dcu variable - Edit Dasharo configuration variables\n"
echo

else
printf "dcu variable - Edit Dasharo configuration variables\n"
echo

fi

printf "dcu variable - Edit Dasharo configuration variables\n\n"
printf "Alias: v\n"
echo

Expand Down Expand Up @@ -299,7 +255,7 @@ dcu_variable_usage() {
# :command.normalize_input
# :command.normalize_input_function
normalize_input() {
local arg flags passthru
local arg passthru flags
passthru=false

while [[ $# -gt 0 ]]; do
Expand Down Expand Up @@ -381,47 +337,59 @@ dcu_mac_command() {
DASHARO_ROM="${args[dasharo_rom_file]}"
MAC="${args[--set]}"
GBE_FLASHREGION_FILENAME="flashregion_3_gbe.bin"

GBE_FLASHREGION_PATH=$GBE_FLASHREGION_FILENAME
set_mac() {
local _mac="$1"
if "${NVMTOOL}" "$GBE_FLASHREGION_FILENAME" copy 0; then
if "${NVMTOOL}" "$GBE_FLASHREGION_PATH" copy 0; then
echo "Copying region 0 to region 1"
else
echo "Failed to copy region 0 to region 1"
if "${NVMTOOL}" "$GBE_FLASHREGION_FILENAME" copy 1; then
if "${NVMTOOL}" "$GBE_FLASHREGION_PATH" copy 1; then
echo "Copying region 1 to region 0"
else
echo "Failed to copy region 1 to region 0"
echo "Both regions are invalid, aborting"
cleanup
return 23
fi
fi

"${NVMTOOL}" "$GBE_FLASHREGION_FILENAME" setmac "${_mac}" &> /dev/null || { echo "Failed to write MAC" ; return 22; }
"${IFDTOOL}" -i gbe:"$GBE_FLASHREGION_FILENAME" "${DASHARO_ROM}" &> /dev/null || { echo "Failed to insert gbe to the binary" ; return 21; }
"${NVMTOOL}" "$GBE_FLASHREGION_PATH" setmac "${_mac}" 1> /dev/null || { cleanup; return 22; }
"${IFDTOOL}" -i gbe:"$GBE_FLASHREGION_PATH" "${DASHARO_ROM}" 1> /dev/null || { cleanup; return 21; }
echo "Moving ${DASHARO_ROM}.new to ${DASHARO_ROM}"
mv "${DASHARO_ROM}.new" "${DASHARO_ROM}" -f
echo "Success"
}

get_mac() {
# dump sections
"${NVMTOOL}" "$GBE_FLASHREGION_FILENAME" dump
dump=$("${NVMTOOL}" "$GBE_FLASHREGION_PATH" "dump" 2>&1 || true)
bad=$(printf "%s" "$dump" | grep "BAD checksum" 2>&1 || true)
dump=$(echo "$dump" | sed "/BAD checksum in part 0/d" | sed "/BAD checksum in part 1/d")
echo "$bad"
echo "$dump"
echo "Success"
}

init() {
"${IFDTOOL}" -x "${DASHARO_ROM}" &> /dev/null || { echo "Failed to extract sections"; }
if [[ ! -f "$GBE_FLASHREGION_FILENAME" ]]; then
if [[ ! -f "${DASHARO_ROM}" ]]; then
echo "Error, file does not exist: ${DASHARO_ROM}"
cleanup
return 1
fi
"${IFDTOOL}" -x "${DASHARO_ROM}" &> /dev/null
if [[ ! -f "$GBE_FLASHREGION_PATH" ]]; then
echo "Managing the MAC address in this binary is currently not supported"
cleanup
return 21
fi
}

cleanup() {
rm -f flashregion*
}

DASHARO_ROM=$(realpath -- "$DASHARO_ROM")
GBE_FLASHREGION_PATH=$(realpath -- "$GBE_FLASHREGION_PATH")
echo "Using ${DASHARO_ROM}"

init
Expand Down Expand Up @@ -538,7 +506,7 @@ dcu_logo_command() {
LOGO_FILE="${args[--logo]}"

if [ ! -f "${LOGO_FILE}" ]; then
echo "Logo file not found or invalid path."
echo "Logo file not found or invalid path: ${LOGO_FILE}"
exit 12
fi

Expand All @@ -554,7 +522,7 @@ dcu_logo_command() {
fi

echo "Setting ${LOGO_FILE} as custom logo"
convert -background None ${LOGO_FILE} BMP3:/tmp/logo.bmp
convert -background None ${LOGO_FILE} BMP3:/tmp/logo.bmp &> /dev/null
# We do not care if this one fails. It can fail if serial_number is not
# already, there, which is fine.
"${CBFSTOOL}" "${DASHARO_ROM}" remove -n logo.bmp -r BOOTSPLASH > /dev/null 2> /dev/null || true
Expand Down Expand Up @@ -634,7 +602,7 @@ dcu_variable_command() {
{
case `typeof $1` in
enum_bool)
_result="$(${SMMSTORETOOL} ${DASHARO_ROM} get -g dasharo -n $1 -t bool)"
_result="$(${SMMSTORETOOL} "${DASHARO_ROM}" get -g dasharo -n $1 -t bool)"
error_check "Variable store was not initialized yet. You need to set some variable first via --set option." 17
if [ "${_result}" = "false" ]; then
echo "Disabled"
Expand All @@ -646,7 +614,7 @@ dcu_variable_command() {
fi
;;
enum_memode)
_result="$(${SMMSTORETOOL} ${DASHARO_ROM} get -g dasharo -n $1 -t uint8)"
_result="$(${SMMSTORETOOL} "${DASHARO_ROM}" get -g dasharo -n $1 -t uint8)"
error_check "Variable store was not initialized yet. You need to set some variable first via --set option." 17
if [ "${_result}" = "0" ]; then
echo "Enabled"
Expand All @@ -660,7 +628,7 @@ dcu_variable_command() {
fi
;;
enum_fancurve)
_result="$(${SMMSTORETOOL} ${DASHARO_ROM} get -g dasharo -n $1 -t uint8)"
_result="$(${SMMSTORETOOL} "${DASHARO_ROM}" get -g dasharo -n $1 -t uint8)"
error_check "Variable store was not initialized yet. You need to set some variable first via --set option." 17
if [ "${_result}" = "0" ]; then
echo "Silent"
Expand All @@ -672,7 +640,7 @@ dcu_variable_command() {
fi
;;
uint8)
_result="$(${SMMSTORETOOL} ${DASHARO_ROM} get -g dasharo -n $1 -t uint8)"
_result="$(${SMMSTORETOOL} "${DASHARO_ROM}" get -g dasharo -n $1 -t uint8)"
error_check "Variable store was not initialized yet. You need to set some variable first via --set option." 17
echo ${_result}
;;
Expand Down Expand Up @@ -743,20 +711,23 @@ dcu_variable_command() {
fi
set_value=${i}
set_type="uint8"
elif [[ $(typeof ${SET}) == "unknown" ]]; then
echo "Variable \"${SET}\" is not supported by the DCU tool yet".
exit 20
else
# All other types: Pass the type and value directly.
set_value=${VALUE}
set_type=$(typeof ${SET})
fi

${SMMSTORETOOL} ${DASHARO_ROM} set -g dasharo -n ${SET} -t ${set_type} -v ${set_value}
${SMMSTORETOOL} "${DASHARO_ROM}" set -g dasharo -n ${SET} -t ${set_type} -v ${set_value}
echo "Successfully set variable ${SET} in the variable store."
}

list_variables()
{
echo "Settings in ${DASHARO_ROM}:"
variables=$(${SMMSTORETOOL} ${DASHARO_ROM} list | grep "dasharo" | sed 's/.*://; s/(.*//')
variables=$(${SMMSTORETOOL} "${DASHARO_ROM}" list | grep "dasharo" | sed 's/.*://; s/(.*//')
tabs 30
echo -e "NAME\tVALUE\tACCEPTED VALUES"
for var in $variables; do
Expand Down Expand Up @@ -1005,6 +976,7 @@ dcu_mac_parse_requirements() {
# :command.required_args_filter
if [[ -z ${args['dasharo_rom_file']+x} ]]; then
printf "missing required argument: DASHARO_ROM_FILE\nusage: dcu mac DASHARO_ROM_FILE [OPTIONS]\n" >&2

exit 1
fi

Expand Down Expand Up @@ -1088,6 +1060,7 @@ dcu_smbios_parse_requirements() {
# :command.required_args_filter
if [[ -z ${args['dasharo_rom_file']+x} ]]; then
printf "missing required argument: DASHARO_ROM_FILE\nusage: dcu smbios DASHARO_ROM_FILE [OPTIONS]\n" >&2

exit 1
fi

Expand Down Expand Up @@ -1157,6 +1130,7 @@ dcu_logo_parse_requirements() {
# :command.required_args_filter
if [[ -z ${args['dasharo_rom_file']+x} ]]; then
printf "missing required argument: DASHARO_ROM_FILE\nusage: dcu logo DASHARO_ROM_FILE [OPTIONS]\n" >&2

exit 1
fi

Expand Down Expand Up @@ -1276,6 +1250,7 @@ dcu_variable_parse_requirements() {
# :command.required_args_filter
if [[ -z ${args['dasharo_rom_file']+x} ]]; then
printf "missing required argument: DASHARO_ROM_FILE\nusage: dcu variable DASHARO_ROM_FILE [OPTIONS]\n" >&2

exit 1
fi

Expand Down
36 changes: 35 additions & 1 deletion dcuc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@

# Dasharo Configuration Utility Container

SCRIPT_DIR=$(dirname "$(readlink -f ${BASH_SOURCE[0]})")

DOCKER_IMAGE="ghcr.io/dasharo/dasharo-sdk:v1.5.0"

docker run -t --rm -v "$(pwd):/app" -w /app $DOCKER_IMAGE ./dcu "$@"
# Initialize an array to store Docker arguments
DOCKER_ARGS=()

# Initialize an array to store the command to be executed in the container
COMMAND_ARGS=()

# Function to add a bind mount for a file
add_bind_mount() {
local FILE_PATH=$1
local ABS_PATH=""
local DIR_PATH=""
ABS_PATH=$(realpath "$FILE_PATH")
DIR_PATH=$(dirname "$ABS_PATH")
DOCKER_ARGS+=("-v" "$DIR_PATH:$DIR_PATH")
echo "$ABS_PATH"
}

# Iterate over the command line arguments
for ARG in "$@"; do
if [[ -f "$ARG" ]]; then
# Convert the file path to an absolute path and add a bind mount
ABS_PATH=$(add_bind_mount "$ARG")
COMMAND_ARGS+=("$ABS_PATH")
else
COMMAND_ARGS+=("$ARG")
fi
done

if [[ -v CI ]]; then
../dcu "${COMMAND_ARGS[@]}"
else
docker run -t --rm -v "${SCRIPT_DIR}:${SCRIPT_DIR}" "${DOCKER_ARGS[@]}" -w "${SCRIPT_DIR}" $DOCKER_IMAGE ./dcu "${COMMAND_ARGS[@]}"
fi
2 changes: 1 addition & 1 deletion src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ commands:
help: Set mac address
examples:
- dcu mac coreboot.rom --set AB:CD:EF:01:23:45
- dcu mac coreboot.rom --get
- dcu mac coreboot.rom
- name: smbios
alias: s
help: Edit SMBIOS data in a firmware image
Expand Down
4 changes: 2 additions & 2 deletions src/logo_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DASHARO_ROM="${args[dasharo_rom_file]}"
LOGO_FILE="${args[--logo]}"

if [ ! -f "${LOGO_FILE}" ]; then
echo "Logo file not found or invalid path."
echo "Logo file not found or invalid path: ${LOGO_FILE}"
exit 12
fi

Expand All @@ -24,7 +24,7 @@ if ! "${CBFSTOOL}" "${DASHARO_ROM}" layout -w | grep -q "BOOTSPLASH"; then
fi

echo "Setting ${LOGO_FILE} as custom logo"
convert -background None ${LOGO_FILE} BMP3:/tmp/logo.bmp
convert -background None ${LOGO_FILE} BMP3:/tmp/logo.bmp &> /dev/null
# We do not care if this one fails. It can fail if serial_number is not
# already, there, which is fine.
"${CBFSTOOL}" "${DASHARO_ROM}" remove -n logo.bmp -r BOOTSPLASH > /dev/null 2> /dev/null || true
Expand Down
Loading