From e13a4fc121809693f2e4ec07e7e29d9e8fb0d5cc Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 29 Apr 2022 15:47:02 +0200 Subject: [PATCH] File for live patching to burn custom VID/PID. Signed-off-by: Pol Henarejos --- patch_vidpid.sh | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 patch_vidpid.sh diff --git a/patch_vidpid.sh b/patch_vidpid.sh new file mode 100755 index 0000000..1f9b633 --- /dev/null +++ b/patch_vidpid.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +# +# This file is part of the Pico OpenPGP distribution (https://github.com/polhenarejos/pico-openpgp). +# Copyright (c) 2022 Pol Henarejos. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +VERSION_MAJOR="1" #Version of Pico CCID Core +VERSION_MINOR="1" + +echo "----------------------------" +echo "VID/PID patcher for Pico OpenPGP" +echo "----------------------------" +echo "" + +if [ "$#" -le 0 ]; then + echo "Usage: $0 VID:PID [input_uf2_file] [output_uf2_file]" + exit 1 +fi + +IFS=':' read -r -a ARR <<< "$1" + +if [ ${#ARR[@]} -ne 2 ]; then + echo "ERROR: Specify vendor and product ids as VID:PID (e.g., $0 CAFE:1234)" + exit 1 +fi + +VID=${ARR[0]} +PID=${ARR[1]} + +if [ ${#VID} -ne 4 ]; then + echo "ERROR: VID length must be 4 hexadecimal characters" + exit 1 +fi + +if [ ${#PID} -ne 4 ]; then + echo "ERROR: PID length must be 4 hexadecimal characters" + exit 1 +fi + +if ! [[ $VID =~ ^[0-9A-Fa-f]{1,}$ ]] ; then + echo "ERROR: VID must contain hexadecimal characters" + exit 1 +fi + +if ! [[ $PID =~ ^[0-9A-Fa-f]{1,}$ ]] ; then + echo "ERROR: PID must contain hexadecimal characters" + exit 1 +fi + +UF2_FILE_IF="pico_openpgp.uf2" +UF2_FILE_OF="$UF2_FILE_IF" + +if [ "$#" -ge 2 ]; then + UF2_FILE_IF="$2" + UF2_FILE_OF="$UF2_FILE_IF" +fi + +if [ "$#" -ge 3 ]; then + UF2_FILE_OF="$3" +fi + + +echo -n "Patching ${UF2_FILE_IF}... " + +if [[ ! -f "$UF2_FILE_IF" ]]; then + echo "ERROR: UF2 file ${UF2_FILE_IF} does not exist" + exit 1 +fi + +if [ "$UF2_FILE_IF" != "$UF2_FILE_OF" ]; then + cp -R $UF2_FILE_IF $UF2_FILE_OF +fi + +LITTLE_VID="\x${VID:2:2}\x${VID:0:2}" +LITTLE_PID="\x${PID:2:2}\x${PID:0:2}" + +perl -pi -e "s/\xff\xfe\xfd\xfc\x$VERSION_MINOR\x$VERSION_MAJOR\x01\x02\x03\x01/$LITTLE_VID$LITTLE_PID\x$VERSION_MINOR\x$VERSION_MAJOR\x01\x02\x03\x01/" $UF2_FILE_OF + +echo "Done!" +echo "" +echo "Patched file was saved in ${UF2_FILE_OF}"