use the Descriptions file to generate most of the static rules

pull/36/head
Hamish Coleman 7 years ago
parent 96485bb94d
commit b9b1e2977f

@ -15,15 +15,22 @@
#
# The fields are:
# filename - The filename that this entry describes
# generator - This field is a tag and a value - see below
# tags - This field is a tag and a value - see below
# description - A free form english description
#
# The generator field is used in the process of creating the file.
# It consists of a tag and a value, separated by a colon. The contents
# of the value will change for different tags
# The tags field is used to store the checksum and to automatically
# build the make rules for this file. It consists of a series of tags
# values pairs which are separated from other pairs with commas.
# Each tag is separated from its value with a colon and each value can
# contain multiple items, separated by semi-colons
#
# Available generator field tags:
# sha1 - the remainder of the field is the known sha1 of the file
# sha1 - The value is the known sha1 of the file
# rule - The basename of the make macro for this file
# dep - The file(s) that this file depends on - the first dep
# file is used to build the inverse rule for this line
# param - Any parameters that are needed to be passed to the macro
#
# The iso images downloadable from Lenovo
# (these checksums are those found on the Lenovo site)
@ -58,25 +65,29 @@ r02uj46d.iso sha1:876a68add32d9c961cbdf80c74b88f92a66443cf x260 BIOS 1.19 (R02E
r0iuj09wd.iso sha1:21b3abf69b9ee7ecf12fe0880564312122b1ff75 x270 BIOS 1.16 (R0IET38W) EC 1.12 (R0IHT30W)
# The firmware files from inside the ISO images
t430.G1HT34WW.s01D2000.FL2 FIXME t430 EC 1.12 Flash File (unpatched)
t430.G1HT35WW.s01D2000.FL2 sha1:617a85d1a4df92ef1171369d6c8a168b8e864e87 t430 EC 1.13 Flash File (unpatched)
t430s.G7HT39WW.s01D8000.FL2 sha1:c0b5b12c3ad63c6acc752cbf77f05f43df7d2502 t430s EC 1.15 Flash File (unpatched)
t530.G4HT39WW.s01D5100.FL2 sha1:071099bcad69b0f54448ab93b4edd0f7ed214d8a t530 EC 1.13 Flash File (unpatched)
w530.G4HT39WW.s01D5200.FL2 sha1:071099bcad69b0f54448ab93b4edd0f7ed214d8a w530 EC 1.13 Flash File (unpatched)
x220.8DHT34WW.s01CB000.FL2 sha1:770588d03c2dcccc833ef911f6247519cfbd6701 x220 EC 1.24 Flash File (unpatched)
x230.G2HT35WW.s01D3000.FL2 sha1:55d419655d849334feb535c51a62795153e6af7f x230 EC 1.14 Flash File (unpatched)
x230t.GCHT25WW.s01DA000.FL2 sha1:854d2b6a75060b9c8257cffc72f101e0b98e205b x230t EC 1.14 Flash File (unpatched)
x250.N10HT17W.s01E5000.FL2 sha1:0732e80cddd0efcca9ba72dcfbdd0110d3c29dae x250 EC 1.16 Flash File (unpatched)
x260.R02HT29W.s0AR0200.FL2 sha1:35a9b0d623fea82a440aae816a91da2cb18f6801 x260 EC 1.11 Flash File (unpatched)
t430.G1HT34WW.s01D2000.FL2 rule:FL2,dep:g1uj25us.iso;g1uj25us.iso.bat,param:01D2000.FL2 t430 EC 1.12 Flash File
t430.G1HT35WW.s01D2000.FL2 rule:FL2,dep:g1uj40us.iso;g1uj40us.iso.bat,param:01D2000.FL2 t430 EC 1.13 Flash File
t430s.G7HT39WW.s01D8000.FL2 rule:FL2,dep:g7uj19us.iso;g7uj19us.iso.bat,param:01D8000.FL2 t430s EC 1.15 Flash File
t530.G4HT39WW.s01D5100.FL2 rule:FL2,dep:g4uj30us.iso;g4uj30us.iso.bat,param:01D5100.FL2 t530 EC 1.13 Flash File
w530.G4HT39WW.s01D5200.FL2 rule:FL2,dep:g5uj28us.iso;g5uj28us.iso.bat,param:01D5200.FL2 w530 EC 1.13 Flash File
x220.8DHT34WW.s01CB000.FL2 rule:FL2,dep:8duj27us.iso;8duj27us.iso.bat,param:01CB000.FL2 x220 EC 1.24 Flash File
x230.G2HT35WW.s01D3000.FL2 rule:FL2,dep:g2uj25us.iso;g2uj25us.iso.bat,param:01D3000.FL2 x230 EC 1.14 Flash File
x230t.GCHT25WW.s01DA000.FL2 rule:FL2,dep:gcuj24us.iso;gcuj24us.iso.bat,param:01DA000.FL2 x230t EC 1.14 Flash File
x250.N10HT17W.s01E5000.FL2 rule:FL2,dep:n10ur10w.iso;n10ur10w.iso.bat,param:01E5000.FL2 x250 EC 1.16 Flash File
x260.R02HT29W.s0AR0200.FL2 rule:FL2,dep:r02uj46d.iso;r02uj46d.iso.bat,param:0AR0200.FL2 x260 EC 1.11 Flash File
# TODO
# - those deps on the .iso.bat files are both ugly and in some cases wrong
# The actual EC firmware extracted from the BIOS firmware file
t430.G1HT34WW.img sha1:d3fde28cbdcb628da23ff30ee6c4a57d976700bf t430 EC 1.12 (decrypted)
t430.G1HT35WW.img sha1:08ab64a0e61865781466fb2bfd97210fe0651bc8 t430 EC 1.13 (decrypted)
t430s.G7HT39WW.img sha1:6e68545a76b42d534c8a4b24a63bf7bece996522 t430s EC 1.15 (decrypted)
t530.G4HT39WW.img sha1:85257101482d8ec7a70d860e176a5fff805fd572 t530 EC 1.13 (decrypted)
w530.G4HT39WW.img sha1:85257101482d8ec7a70d860e176a5fff805fd572 w530 EC 1.13 (decrypted)
x220.8DHT34WW.img.enc sha1:5dccc0284991ef5144e2b9a114249edb77fa0b28 x220 EC 1.24 (encrypted)
x230.G2HT35WW.img sha1:d70f5434ef316a66a6195651d9e231e84a2464a1 x230 EC 1.14 (decrypted)
x230t.GCHT25WW.img sha1:d9db308756a6a82ff83ee8e3eae930b5ff550e28 x230t EC 1.14 (decrypted)
x250.N10HT17W.img.enc FIXME x250 EC 1.16 (encrypted)
x260.R02HT29W.img FIXME x260 EC 1.11 (not encrypted)
t430.G1HT34WW.img rule:IMG,dep:t430.G1HT34WW.s01D2000.FL2 t430 EC 1.12 (decrypted)
t430.G1HT35WW.img rule:IMG,dep:t430.G1HT35WW.s01D2000.FL2 t430 EC 1.13 (decrypted)
t430s.G7HT39WW.img rule:IMG,dep:t430s.G7HT39WW.s01D8000.FL2 t430s EC 1.15 (decrypted)
t530.G4HT39WW.img rule:IMG,dep:t530.G4HT39WW.s01D5100.FL2 t530 EC 1.13 (decrypted)
w530.G4HT39WW.img rule:IMG,dep:w530.G4HT39WW.s01D5200.FL2 w530 EC 1.13 (decrypted)
x220.8DHT34WW.img.enc rule:IMGnoenc,dep:x220.8DHT34WW.s01CB000.FL2 x220 EC 1.24 (encrypted)
x230.G2HT35WW.img rule:IMG,dep:x230.G2HT35WW.s01D3000.FL2 x230 EC 1.14 (decrypted)
x230t.GCHT25WW.img rule:IMG,dep:x230t.GCHT25WW.s01DA000.FL2 x230t EC 1.14 (decrypted)
x250.N10HT17W.img.enc rule:IMGnoenc,dep:x250.N10HT17W.s01E5000.FL2 x250 EC 1.16 (encrypted)
x260.R02HT29W.img rule:IMGnoenc,dep:x260.R02HT29W.s0AR0200.FL2 x260 EC 1.11 (not encrypted)

@ -1,7 +1,7 @@
#
# Infrastructure to manage patching thinkpad EC firmware
#
# Copyright (C) 2016 Hamish Coleman
# Copyright (C) 2016-2017 Hamish Coleman
#
all: list_laptops
@ -299,92 +299,89 @@ mec-tools/mec_encrypt: mec-tools/Makefile
git submodule update
make -C mec-tools
# using function calls to build rules with actions is kind of a hack,
# which is why these are all on oneline.
# Insert the new firmware into the FL2 file
# $1 = encoded EC firmware
# $2 = FL2 filename
define rule_fl2_patch
$(2): $(1) ; cp --reflink=auto $(2).orig $(2) && ./scripts/FL2_copyIMG to_fl2 $(2) $(1)
endef
# Create a new ISO image with patches applied
# $1 = FL2 linux filename
# $2 = pattern to match FL2 file in ISO image
# $3 = ISO image
define rule_iso
$(3): $(1) $(3).bat ; cp --reflink=auto $(3).orig $(3) && ./scripts/copyFL2 to_iso $(3) $(1) $(2) && sed -i "s/__BUILT/`sha1sum $(1)`/" $(3).bat && mcopy -m -o -i $(3)@@$(FAT_OFFSET) $(3).bat ::AUTOEXEC.BAT && mdel -i $(3)@@$(FAT_OFFSET) ::EFI/Boot/BootX64.efi
endef
# TODO - remove any FL1 files from the image, ensuring it can never
# accidentally be used for BIOS updates
# Extract the FL2 file from an ISO image
# Note that the parameters here are intentionally the same as rule_iso,
# but you cannot define two targets with one define..
# $1 = FL2 linux filename basename
# $2 = pattern to match FL2 file in ISO image
# $3 = ISO image basename
#
# Note that the integrity of the FL2 file is determined by two things:
# - The sha1sum for the iso.orig file has been checked
# - The sha1sum for the ISO.orig file has been checked
# - The ./scripts/copyFL2 script is generating correct data
# We believe these two statements are correct, so there is no need to check
# the checksum for the extracted FL2.orig file
define rule_fl2_extract
$(1).orig: $(3).orig ; ./scripts/copyFL2 from_iso $(3).orig $(1).orig $(2)
#
# $@ is the FL2 file to create
# $< is the ISO file
# $1 is the pattern to match FL2 file in ISO image
define rule_FL2_extract
./scripts/copyFL2 from_iso $< $@ $(1)
endef
rule_FL2_extract_DEPS = scripts/copyFL2
# Extract the IMG file from an FL2 file
# Note that the parameters here are intentionallty the same as rule_fl2_patch,
# but you cannot define two targets with one define..
# $1 = IMG file basename
# $2 = FL2 file basename
define rule_img_extract
$(1).orig: $(2).orig ./scripts/FL2_copyIMG ; ./scripts/FL2_copyIMG from_fl2 $(2).orig $(1).orig
# Extract and decyrpt the IMG file from an FL2 file
#
# $@ is the decrypted IMG to create
# $< is the FL2
define rule_IMG_extract
./scripts/FL2_copyIMG from_fl2 $< $(subst .orig,.enc.tmp,$@)
mec-tools/mec_encrypt -d $(subst .orig,.enc.tmp,$@) $@
rm $(subst .orig,.enc.tmp,$@)
mec-tools/mec_csum_flasher -c $@
mec-tools/mec_csum_boot -c $@
endef
rule_IMG_extract_DEPS = scripts/FL2_copyIMG mec-tools/mec_encrypt mec-tools/mec_csum_flasher mec-tools/mec_csum_boot
# Create a new ISO image with patches applied
#
# TODO:
# - add a simple method to autogenerate these non-generic rules
# - this is getting closer with the replacement of the .slice files with
# generic scripts.
# - once that is done, convert the defines back to action bodies, not
# rule definitions
# Hacky, non generic rules
$(call rule_fl2_patch,t430.G1HT34WW.img.enc,t430.G1HT34WW.s01D2000.FL2)
$(call rule_fl2_patch,t430.G1HT35WW.img.enc,t430.G1HT35WW.s01D2000.FL2)
$(call rule_fl2_patch,t430s.G7HT39WW.img.enc,t430s.G7HT39WW.s01D8000.FL2)
$(call rule_fl2_patch,t530.G4HT39WW.img.enc,t530.G4HT39WW.s01D5100.FL2)
$(call rule_fl2_patch,w530.G4HT39WW.img.enc,w530.G4HT39WW.s01D5200.FL2)
$(call rule_fl2_patch,x230.G2HT35WW.img.enc,x230.G2HT35WW.s01D3000.FL2)
$(call rule_fl2_patch,x230t.GCHT25WW.img.enc,x230t.GCHT25WW.s01DA000.FL2)
$(call rule_img_extract,x220.8DHT34WW.img.enc,x220.8DHT34WW.s01CB000.FL2)
$(call rule_img_extract,t430.G1HT34WW.img.enc,t430.G1HT34WW.s01D2000.FL2)
$(call rule_img_extract,t430.G1HT35WW.img.enc,t430.G1HT35WW.s01D2000.FL2)
$(call rule_img_extract,t430s.G7HT39WW.img.enc,t430s.G7HT39WW.s01D8000.FL2)
$(call rule_img_extract,t530.G4HT39WW.img.enc,t530.G4HT39WW.s01D5100.FL2)
$(call rule_img_extract,w530.G4HT39WW.img.enc,w530.G4HT39WW.s01D5200.FL2)
$(call rule_img_extract,x230.G2HT35WW.img.enc,x230.G2HT35WW.s01D3000.FL2)
$(call rule_img_extract,x230t.GCHT25WW.img.enc,x230t.GCHT25WW.s01DA000.FL2)
$(call rule_iso,t430.G1HT34WW.s01D2000.FL2,01D2000.FL2,g1uj25us.iso)
$(call rule_iso,t430.G1HT35WW.s01D2000.FL2,01D2000.FL2,g1uj40us.iso)
$(call rule_iso,x230.G2HT35WW.s01D3000.FL2,01D3000.FL2,g2uj25us.iso)
$(call rule_iso,t530.G4HT39WW.s01D5100.FL2,01D5100.FL2,g4uj30us.iso)
$(call rule_iso,w530.G4HT39WW.s01D5200.FL2,01D5200.FL2,g5uj28us.iso)
$(call rule_iso,t430s.G7HT39WW.s01D8000.FL2,01D8000.FL2,g7uj19us.iso)
$(call rule_iso,x230t.GCHT25WW.s01DA000.FL2,01DA000.FL2,gcuj24us.iso)
$(call rule_fl2_extract,x220.8DHT34WW.s01CB000.FL2,01CB000.FL2,8duj27us.iso)
$(call rule_fl2_extract,t430.G1HT35WW.s01D2000.FL2,01D2000.FL2,g1uj40us.iso)
$(call rule_fl2_extract,t430.G1HT34WW.s01D2000.FL2,01D2000.FL2,g1uj25us.iso)
$(call rule_fl2_extract,x230.G2HT35WW.s01D3000.FL2,01D3000.FL2,g2uj25us.iso)
$(call rule_fl2_extract,t530.G4HT39WW.s01D5100.FL2,01D5100.FL2,g4uj30us.iso)
$(call rule_fl2_extract,w530.G4HT39WW.s01D5200.FL2,01D5200.FL2,g5uj28us.iso)
$(call rule_fl2_extract,t430s.G7HT39WW.s01D8000.FL2,01D8000.FL2,g7uj19us.iso)
$(call rule_fl2_extract,x230t.GCHT25WW.s01DA000.FL2,01DA000.FL2,gcuj24us.iso)
$(call rule_fl2_extract,x260.R02HT29W.s0AR0200.FL2,0AR0200.FL2,r02uj46d.iso)
$(call rule_fl2_extract,x270.R0IHT30W.s0AR0I00.FL2,0AR0I00.FL2,r0iuj09wd.iso)
# $@ is the ISO to create
# $< is the FL2
# $1 is the pattern to match FL2 file in ISO image
define rule_FL2_insert
cp --reflink=auto $@.orig $@.tmp
./scripts/copyFL2 to_iso $@.tmp $< $(1)
sed -i "s/__BUILT/`sha1sum $<`/" $@.bat
mcopy -m -o -i $@.tmp@@$(FAT_OFFSET) $@.bat ::AUTOEXEC.BAT
mdel -i $@.tmp@@$(FAT_OFFSET) ::EFI/Boot/BootX64.efi
mv $@.tmp $@
endef
rule_FL2_insert_DEPS = scripts/copyFL2 # TODO - bat file
# Insert the new firmware into the FL2 file
#
# $@ is the FL2 to create
# $< is the IMG
define rule_IMG_insert
./scripts/xx30.encrypt $< $<.enc.tmp
cp --reflink=auto $@.orig $@.tmp
./scripts/FL2_copyIMG to_fl2 $@.tmp $<.enc.tmp
rm $<.enc.tmp
mv $@.tmp $@
endef
rule_IMG_insert_DEPS = scripts/FL2_copyIMG scripts/xx30.encrypt
# Extract the IMG file from an FL2 file - special case, without decryption
#
# $@ is the IMG to create
# $< is the FL2
define rule_IMGnoenc_extract
./scripts/FL2_copyIMG from_fl2 $< $@
endef
rule_IMGnoenc_extract_DEPS = scripts/FL2_copyIMG
# Insert the new firmware into the FL2 file - special case, without encryption
#
# $@ is the FL2 to create
# $< is the IMG
define rule_IMGnoenc_insert
cp --reflink=auto $@.orig $@.tmp
./scripts/FL2_copyIMG to_fl2 $@.tmp $<
mv $@.tmp $@
endef
rule_IMGnoenc_insert_DEPS = scripts/FL2_copyIMG
# Generate and include the rules that use the above macros
-include $(DEPSDIR)/generated.deps
CLEAN_FILES += $(DEPSDIR)/generated.deps
$(DEPSDIR)/generated.deps: scripts/generate_deps
$(DEPSDIR)/generated.deps: Descriptions.txt
./scripts/generate_deps $< >$@

@ -5,6 +5,7 @@
#
# FIXME
# - the description file is expected to be in the current dir
# - does not handle description file entries with more than just the sha1 tag
DESCRIPTIONS='Descriptions.txt'

@ -0,0 +1,108 @@
#!/usr/bin/env perl
use warnings;
use strict;
#
# Read through a descriptions file on input and auto generate a list of rules
# for building the various files on output
#
# Each rule input line generates two dependancies:
# $filename.orig from $depname.orig using macro rule_$rule_extract
# $depname from $filename using macro rule_$rule_insert
#
my @entries;
while(<>) {
s/^#.*//; # remove comment lines
next if (m/^$/);
if (!m/^(\S+)\s+(\S+)\s+(.*)/) {
# if we dont have three space-separated fields, skip the line
next;
}
my $entry = {};
$entry->{generator_file} = $ARGV;
$entry->{generator_line} = $.;
$entry->{filename} = $1;
$entry->{tags_raw} = $2;
$entry->{description} = $3;
for my $tv (split(/,/,$entry->{tags_raw})) {
my ($tag,$val_raw) = split(/:/,$tv);
my @values = split(/;/,$val_raw);
push @{$entry->{tags}{$tag}}, @values;
}
push @entries,$entry;
}
my $rules_found = 0;
my @extracts;
my @inserts;
for my $entry (@entries) {
# only generate rules for lines that ask for them
next if (!defined($entry->{tags}{rule}));
# FIXME - if there is more than one rule, error?
# currently need at least one dep defined
next if (!defined($entry->{tags}{dep}));
$rules_found++;
my @deps = @{$entry->{tags}{dep}};
my $dep0 = shift @deps;
my @params;
if (defined($entry->{tags}{param})) {
@params = @{$entry->{tags}{param}};
}
# generate the forward rule
my $rule = "rule_".$entry->{tags}{rule}[0]."_extract";
push @extracts,
sprintf("# %s:%i\n",
$entry->{generator_file},
$entry->{generator_line},
),
sprintf("%s: %s %s %s %s\n\t\$(call %s)\n\n",
$entry->{filename}.".orig",
$dep0.".orig",
join(' ',@deps),
$entry->{generator_file},
'$('.$rule.'_DEPS)',
join(',',$rule,@params),
);
# generate the inverse rule
$rule = "rule_".$entry->{tags}{rule}[0]."_insert";
push @inserts,
sprintf("# %s:%i\n",
$entry->{generator_file},
$entry->{generator_line},
),
sprintf("%s: %s %s %s %s\n\t\$(call %s)\n\n",
$dep0,
$entry->{filename},
join(' ',@deps),
$entry->{generator_file},
'$('.$rule.'_DEPS)',
join(',',$rule,@params),
);
}
if (!$rules_found) {
# only generate output if we have at least one matching line - so an empty input
# will just result in an empty output
exit(0);
}
print("# autogenerated rules file - do not edit\n\n");
print(join('',@extracts));
print("\n\n");
print(join('',@inserts));
Loading…
Cancel
Save