From b39f4c38ebedfec6828238824fa048251cfd0dbf Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 20 Apr 2022 17:15:46 +0800 Subject: [PATCH] fix update scripts so updates don't wipe root keys :-O --- tools/factory_reset.ps1 | 2 +- tools/factory_reset.sh | 2 +- tools/update_ci.ps1 | 2 +- tools/update_ci.sh | 2 +- tools/update_release.ps1 | 2 +- tools/usb_update.py | 34 +++++++++++++++++++++++++++++++--- 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/tools/factory_reset.ps1 b/tools/factory_reset.ps1 index 34db9dd58..4fc438adb 100644 --- a/tools/factory_reset.ps1 +++ b/tools/factory_reset.ps1 @@ -40,7 +40,7 @@ Start-Sleep 5 Invoke-WebRequest https://ci.betrusted.io/releases/latest/soc_csr.bin -OutFile soc_csr.bin Invoke-WebRequest https://ci.betrusted.io/releases/latest/loader.bin -OutFile loader.bin -python usb_update.py --enable-boot --soc soc_csr.bin -l loader.bin +python usb_update.py --enable-boot-wipe --soc soc_csr.bin -l loader.bin Remove-Item soc_csr.bin Remove-Item loader.bin diff --git a/tools/factory_reset.sh b/tools/factory_reset.sh index bec0c0473..90c8f76b5 100755 --- a/tools/factory_reset.sh +++ b/tools/factory_reset.sh @@ -56,7 +56,7 @@ sleep 5 # this should sequence last because it overwrites all of the CSR values on the currently loaded SoC wget https://ci.betrusted.io/$REVISION/soc_csr.bin -O /tmp/soc_csr.bin wget https://ci.betrusted.io/$REVISION/loader.bin -O /tmp/loader.bin -./usb_update.py --enable-boot --soc /tmp/soc_csr.bin -l /tmp/loader.bin +./usb_update.py --enable-boot-wipe --soc /tmp/soc_csr.bin -l /tmp/loader.bin rm /tmp/loader.bin rm /tmp/soc_csr.bin diff --git a/tools/update_ci.ps1 b/tools/update_ci.ps1 index 84c0fc527..0f9a5ae7c 100644 --- a/tools/update_ci.ps1 +++ b/tools/update_ci.ps1 @@ -41,7 +41,7 @@ Start-Sleep 5 Invoke-WebRequest https://ci.betrusted.io/latest-ci/loader.bin -OutFile loader.bin Invoke-WebRequest https://ci.betrusted.io/latest-ci/soc_csr.bin -OutFile soc_csr.bin -python usb_update.py --enable-boot --soc soc_csr.bin -l loader.bin +python usb_update.py --enable-boot-update -s soc_csr.bin -l loader.bin Remove-Item loader.bin Remove-Item soc_csr.bin diff --git a/tools/update_ci.sh b/tools/update_ci.sh index 9670e8300..fc8e0fdb4 100755 --- a/tools/update_ci.sh +++ b/tools/update_ci.sh @@ -58,7 +58,7 @@ sleep 5 wget https://ci.betrusted.io/$REVISION/loader.bin -O /tmp/loader.bin wget https://ci.betrusted.io/$REVISION/soc_csr.bin -O /tmp/soc_csr.bin -./usb_update.py --enable-boot --soc /tmp/soc_csr.bin -l /tmp/loader.bin +./usb_update.py --enable-boot-update -s /tmp/soc_csr.bin -l /tmp/loader.bin rm /tmp/loader.bin rm /tmp/soc_csr.bin diff --git a/tools/update_release.ps1 b/tools/update_release.ps1 index da7dceade..f7552e76f 100644 --- a/tools/update_release.ps1 +++ b/tools/update_release.ps1 @@ -41,7 +41,7 @@ Start-Sleep 5 Invoke-WebRequest https://ci.betrusted.io/releases/latest/loader.bin -OutFile loader.bin Invoke-WebRequest https://ci.betrusted.io/releases/latest/soc_csr.bin -OutFile soc_csr.bin -python usb_update.py --enable-boot --soc soc_csr.bin -l loader.bin +python usb_update.py --enable-boot-update -s soc_csr.bin -l loader.bin Remove-Item loader.bin Remove-Item soc_csr.bin diff --git a/tools/usb_update.py b/tools/usb_update.py index 0bcf8490c..a8907f75e 100755 --- a/tools/usb_update.py +++ b/tools/usb_update.py @@ -414,7 +414,10 @@ def main(): "--disable-boot", required=False, action='store_true', help="Disable system boot (for use in multi-stage updates)" ) parser.add_argument( - "--enable-boot", required=False, action='store_true', help="Re-enable system boot. Requires both a loader and a soc spec." + "--enable-boot-wipe", required=False, action='store_true', help="Re-enable system boot for factory reset. Requires both a loader (-l) and a soc (--soc) spec. Overwrites root keys." + ) + parser.add_argument( + "--enable-boot-update", required=False, action='store_true', help="Re-enable system boot for updates. Requires both a loader (-l) and a staging (-s) spec. Stages SOC without overwriting root keys." ) parser.add_argument( "-k", "--kernel", required=False, help="Kernel", type=str, nargs='?', metavar=('kernel file'), const='../target/riscv32imac-unknown-xous-elf/release/xous.img' @@ -558,7 +561,7 @@ def main(): print("Disabling boot") pc_usb.erase_region(locs['LOC_LOADER'][0], 1024 * 256) - if args.enable_boot: + if args.enable_boot_wipe: if args.loader == None: print("Must provide both a loader and soc image") if args.soc == None: @@ -586,6 +589,31 @@ def main(): pass # we expect an error because we reset the SOC and that includes the USB core exit(0) + if args.enable_boot_update: + if args.loader == None: + print("Must provide both a loader and soc image") + if args.staging == None: + print("Must provide both a soc and loader image") + print("Enabling boot with {} and {}".format(args.loader, args.staging)) + print("Programming loader image {}".format(args.loader)) + with open(args.loader, "rb") as f: + image = f.read() + pc_usb.flash_program(locs['LOC_LOADER'][0], image, verify=verify) + print("Staging SoC gateware".format(args.staging)) + with open(args.staging, "rb") as f: + image = f.read() + pc_usb.flash_program(locs['LOC_STAGING'][0], image, verify=verify) + + print("Resuming CPU.") + pc_usb.poke(vexdbg_addr, 0x02000000) + + print("Resetting SOC...") + try: + pc_usb.poke(pc_usb.register('reboot_soc_reset'), 0xac, display=False) + except usb.core.USBError: + pass # we expect an error because we reset the SOC and that includes the USB core + exit(0) + if args.image: image_file, addr_str = args.image addr = int(addr_str, 0) @@ -607,7 +635,7 @@ def main(): pc_usb.flash_program(locs['LOC_WF200'][0], image, verify=verify) if args.staging != None: - print("Programming SoC gateware {}".format(args.soc)) + print("Staging SoC gateware {}".format(args.soc)) with open(args.staging, "rb") as f: image = f.read() pc_usb.flash_program(locs['LOC_STAGING'][0], image, verify=verify)