From c0b2178942bd0df79db20161d3af0edae4c8ef64 Mon Sep 17 00:00:00 2001 From: Hamish Coleman Date: Mon, 3 Jul 2017 18:17:54 +0800 Subject: [PATCH] Remove most of the variables from the autoexec.bat template Instead use a *.report file for each component and add data as we go, culminating in the built image simply catting the report to the screen to show the user what is about to occur. This is on the path towards removing all variables from the autoexec template - which would make it simpler to reuse the same template for other commands (e.g the x200 series does not use dosflash.exe) --- .gitignore | 1 + Makefile | 45 +++++++++++++++++++++++++++++++-------- autoexec.bat.template | 44 ++++++++++++++++++-------------------- scripts/hexpatch.pl | 49 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 105 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 099a840..85a0f66 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ # ignore generated files *.bat +*.report .d # Temp are (should be) just junk diff --git a/Makefile b/Makefile index 55c07bd..cdbde1a 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ $(shell mkdir -p $(DEPSDIR)) clean: rm -f $(CLEAN_FILES) \ patched.*.iso patched.*.img *.FL2 *.FL2.orig *.img.enc \ - *.img.enc.orig *.img.orig *.bat \ + *.img.enc.orig *.img.orig *.bat *.report \ *.img \ *.txt.orig rm -rf *.iso.extract *.iso.orig.extract @@ -195,14 +195,14 @@ endef # Generate a working file with any known patches applied %.img: %.img.orig cp --reflink=auto $< $@ - ./scripts/hexpatch.pl --rm_on_fail $@ $@.d/*.patch + ./scripts/hexpatch.pl --rm_on_fail --report $@.report $@ $@.d/*.patch # using both __DIR and __FL2 is a hack to get around needing to quote the # DOS path separator. It feels like there should be a better way if I put # my mind to it.. # %.iso.bat: %.iso.orig autoexec.bat.template - sed -e "s%__DIR%`mdir -/ -b -i $<@@$(FAT_OFFSET) |grep FL2 |head -1|cut -d/ -f3`%; s%__FL2%`mdir -/ -b -i $<@@$(FAT_OFFSET) |grep FL2 |head -1|cut -d/ -f4`%; s%__DESC%`scripts/describe $(basename $<)`%; s/__BUILDINFO/$(BUILDINFO)/" autoexec.bat.template >$@.tmp + sed -e "s%__DIR%`mdir -/ -b -i $<@@$(FAT_OFFSET) |grep FL2 |head -1|cut -d/ -f3`%; s%__FL2%`mdir -/ -b -i $<@@$(FAT_OFFSET) |grep FL2 |head -1|cut -d/ -f4`%" autoexec.bat.template >$@.tmp mv $@.tmp $@ touch -d @1 $@ @@ -246,14 +246,14 @@ GETELTORITO := ./scripts/geteltorito $(GETELTORITO) -o $@.tmp $< ./scripts/fix_mbr $@.tmp mv $@.tmp $@ - $(call build_info,$<.bat) + $(call build_info,$<.report) # $1 is the lenovo named iso # $2 is the nicely named iso define patched_iso mv $1 $2 - mv $1.bat $2.bat - $(call build_info,$2.bat) + mv $1.report $2.report + $(call build_info,$2.report) endef # $1 is the bat file @@ -261,7 +261,27 @@ define build_info @echo @echo @echo Your build has completed with the following details: - @grep Buil $1 + @echo + @cat $1 +endef + +# Add information about the FL2 file to the current report +# $< is the IMG file +# $@ is the FL2 file being inserted into +define buildinfo_FL2 + echo "Buildinfo: $(BUILDINFO)" >$@.report + echo "Built: `sha1sum $@`" >>$@.report + echo "" >>$@.report + cat $<.report >>$@.report +endef + +# Add information about the ISO file to the current report +# $< is the FL2 file +# $@ is the ISO file being inserted into +define buildinfo_ISO + cp $<.report $@.report + echo "" >>$@.report + echo "Description: `scripts/describe $@`" >>$@.report endef # simple testing of images in an emulator @@ -317,8 +337,9 @@ rule_IMG_extract_DEPS = scripts/FL2_copyIMG mec-tools/mec_encrypt mec-tools/mec_ define rule_FL2_insert cp --reflink=auto $@.orig $@.tmp ./scripts/ISO_copyFL2 to_iso $@.tmp $< $(1) - sed -i "s/__BUILT/`sha1sum $<`/" $@.bat - mcopy -m -o -i $@.tmp@@$(FAT_OFFSET) $@.bat ::AUTOEXEC.BAT + $(call buildinfo_ISO) + mcopy -t -m -o -i $@.tmp@@$(FAT_OFFSET) $@.report ::report.txt + mcopy -t -m -o -i $@.tmp@@$(FAT_OFFSET) $@.bat ::AUTOEXEC.BAT -mdel -i $@.tmp@@$(FAT_OFFSET) ::EFI/Boot/BootX64.efi mv $@.tmp $@ endef @@ -327,6 +348,10 @@ rule_FL2_insert_DEPS = scripts/ISO_copyFL2 # TODO - bat file # - maybe mdel any FL1 files, so the image can not accidentally be used to # flash the BIOS? # - only delete the UEFI updater if it exists in the original ISO +# - continue removing variables from the AUTOEXEC bat - perhaps calculate +# its contents here +# - provide a simple mechanism for selecting the flash command to run, to +# allow for autoexec bat files that do not use dosflash # Insert the new firmware into the FL2 file # @@ -338,6 +363,7 @@ define rule_IMG_insert ./scripts/FL2_copyIMG to_fl2 $@.tmp $<.enc.tmp rm $<.enc.tmp mv $@.tmp $@ + $(call buildinfo_FL2) endef rule_IMG_insert_DEPS = scripts/FL2_copyIMG scripts/xx30.encrypt @@ -360,6 +386,7 @@ define rule_IMGnoenc_insert cp --reflink=auto $@.orig $@.tmp ./scripts/FL2_copyIMG to_fl2 $@.tmp $< mv $@.tmp $@ + $(call buildinfo_FL2) endef rule_IMGnoenc_insert_DEPS = scripts/FL2_copyIMG diff --git a/autoexec.bat.template b/autoexec.bat.template index 295f920..87555cd 100644 --- a/autoexec.bat.template +++ b/autoexec.bat.template @@ -1,24 +1,20 @@ -@ECHO OFF -PROMPT $p$g -cd c:\flash -cls -echo. -echo. -echo This will flash your firmware to support a classic 7-row keyboard -echo. -echo. -echo WARNING: Incorrectly flashing your firmware can damage your laptop. -echo Ensure this is the right firmware version before attempting to flash -echo the firmware - there are minimal checks done. -echo. -echo This disc will flash the file __DIR\__FL2 -echo. -echo This disk is prepared for __DESC -echo. -echo Buildinfo: __BUILDINFO -echo Built: __BUILT -echo. -pause -echo on -dosflash /sd /ipf ec /file __DIR\__FL2 - +@ECHO OFF +PROMPT $p$g +cd c:\flash +cls +echo. +echo. +echo This will flash your Embedded controller firmware +echo. +echo. +echo WARNING: Incorrectly flashing your firmware can damage your laptop. +echo Ensure this is the right firmware version before attempting to flash +echo the firmware - there are minimal checks done. +echo. +echo This disc will flash the file __DIR\__FL2 +echo. +type c:\report.txt +echo. +pause +echo on +dosflash /sd /ipf ec /file __DIR\__FL2 diff --git a/scripts/hexpatch.pl b/scripts/hexpatch.pl index d8821c0..a7bb496 100755 --- a/scripts/hexpatch.pl +++ b/scripts/hexpatch.pl @@ -20,7 +20,7 @@ sub usage() { print("patching the right file\n"); print("\n"); print("Usage:\n"); - print(" hexpatch.pl [--rm_on_fail] binaryfile patchfile [patchfile...]\n"); + print(" hexpatch.pl [--rm_on_fail] [--report file] binaryfile patchfile [patchfile...]\n"); print("\n"); exit(1); } @@ -138,6 +138,7 @@ sub verify_context { $fh->seek($addr,SEEK_SET); $fh->read($found,length($expected)); + # TODO - if found eq new warn "patch reversed?" if ($found ne $expected) { warn("Address $addr mismatched data\n"); return undef; @@ -167,6 +168,40 @@ sub apply_patch { } } +sub output_report { + my $filename = shift; + my @patches = @_; + + my $fh = IO::File->new($filename, "w"); + if (!defined($fh)) { + warn("Could not open report file $filename: $!\n"); + exit(1); + } + + if (!@patches) { + $fh->print("No patches applied: Lenovo default firmware\n"); + return; + } + + $fh->print("Patches applied:\n"); + my $line = ""; + for my $patch (@patches) { + if (length($line)+length($patch)+1 > 79) { + $line .= "\n"; + $fh->print($line); + $line = ""; + } + if ($line) { + $line .= " "; + } + $line .= $patch; + } + if ($line) { + $line .= "\n"; + $fh->print($line); + } +} + sub rm_on_fail { my ($bool, $filename) = @_; @@ -182,6 +217,12 @@ sub main() { shift @ARGV; } + my $reportfile; + if (defined($ARGV[0]) && $ARGV[0] eq "--report") { + shift @ARGV; + $reportfile=shift @ARGV; + } + my $binaryfile = shift @ARGV; if (!defined($binaryfile) or !defined($ARGV[0])) { usage(); @@ -195,6 +236,7 @@ sub main() { print("Attempting to patch $binaryfile\n"); + my @report_patches; while ($ARGV[0]) { my $patchfile = shift @ARGV; @@ -219,6 +261,11 @@ sub main() { print("Applying ",$patchfile," ",$db->{name},"\n"); apply_patch($db,$fh); + push @report_patches,$patchfile; + } + + if ($reportfile) { + output_report($reportfile,@report_patches); } } main();