asuswrt-merlin.ng/release/src-rt-5.02axhnd/hostTools/imagetools/image.emmc.ext
2020-08-30 13:27:47 -04:00

293 lines
11 KiB
Perl

#!/usr/bin/env perl
use strict;
use warnings;
use bytes;
####
#### Assembles images for GEN3 devices with emmc squashfs
####
unless ( $ENV{SECURE_BOOT_ARCH} eq 'GEN3' ) {
die "Not supported $ENV{SECURE_BOOT_ARCH}";
}
sub shell {
#if (defined $_[1]) {
print "$_[0]\n";
#}
my $res = `$_[0]`;
( $? == 0 ) or die "ERROR: $!";
print "$res";
return $res;
}
#
# Staging of the script
#
# Stage 1: prepare - generates authenticated headers and hashes, prepares non-secure cferom then exit
# !!! must be complete by stage 2
#
# Stage 2: complete - expects hashes/headers/bootloader signatures generated outside of the scope of this script
# before this stage is executed
#
print "-- args @ARGV --- \n";
my @args = @ARGV;
# max area reserved for bootloader with header and signature (if secure)
my $image_alloc_size = $ENV{BTRM_IMAGE_SIZE_ALLOCATION} * 1024;
#
# starting offset where first image is placed
my $offset = 65536;
#
# number of unsecured images:
# 2 copies of unsec images + one headerless XIP cferom
my $num_unsec_images = 2;
#
# 2 copies of fld secure images
my $num_sec_images = 2;
#
# we want to insert secure+unsecure images in 1MB
# let's guard against overflow
# account for 65k reserved block from the beginning of the boot area
( ((1024*1024 - $offset) - ($image_alloc_size * ($num_unsec_images + $num_sec_images)) ) >= 0 )
or die "this number of images will not fit to a boot area";
#
# prepare an array of offsets for non-secure images
#
my @unsec_offset = map{ $offset + $image_alloc_size * $_} (0..$num_unsec_images-1);
$offset = $unsec_offset[-1] + $image_alloc_size;
#
# set an array of offsets for secure images
#
my @sec_offset = map{ $offset + $image_alloc_size * $_} (0..$num_sec_images-1);
#
### IF YOU WANT CUSTOM OFFSETS, replace values in @unsec_offset amd @sec_offset
#
print "unsecure images offsets : @unsec_offset \n secure images offsets: @sec_offset \n";
#
# Re-assigning to a local variable to take into account
# an SOTP TurnKey choice
#
#
my $CFE_ROM = ( $ENV{SECURE_BOOT_TURNKEY} eq "y" ) ? $ENV{WDIR}.'/'.$ENV{CFE_ROM_TK_BN} : $ENV{WDIR}.'/'.$ENV{CFE_ROM_BN};
if ( !( $ENV{SECURE_BOOT_ENCRYPT_BTLDRS} eq 'y' ) ) {
$ENV{MFG_ENC} = '';
$ENV{FLD_ENC} = '';
}
if ( ( defined $args[0] ) and $args[0] eq "prepare" ) {
if ( (defined $ENV{BUILD_SECURE_BOOT}) && ($ENV{BUILD_SECURE_BOOT} eq 'y' )) {
#
#
# Create the block of hashes
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/mkhashes \\
--out=$ENV{WDIR}/.hashes.fld --item rootfs=$ENV{PROFILE_DIR}/squashfs.img --file secram.000 --boot $ENV{WDIR}/bootfs"
);
#
# Generate the MFG auth headers for CFEROM
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/insertboot --arch $ENV{SECURE_BOOT_ARCH} --cfe $CFE_ROM$ENV{MFG_ENC} \\
--mfg --cred=$ENV{MFG_CRED_LIST} --chip=$ENV{BRCM_CHIP} --out=$ENV{WDIR}/.header.mfg"
);
#
#
# Create the block of hashes
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/mkhashes \\
--out=$ENV{WDIR}/.hashes.mfg --item rootfs=$ENV{PROFILE_DIR}/squashfs.img --file secmfg.000 --boot $ENV{WDIR}/bootfs"
);
#
# Generate the FLD auth headers for CFEROM
shell(
# adding original non-tk capable CFE_ROM
"$ENV{HOSTTOOLS_DIR}/imagetools/insertboot --arch $ENV{SECURE_BOOT_ARCH} --cfe $ENV{WDIR}/$ENV{CFE_ROM_BN}$ENV{FLD_ENC} \\
--field --cred=$ENV{FLD_CRED_LIST} --chip=$ENV{BRCM_CHIP} --out=$ENV{WDIR}/.header.fld"
);
if ( (defined $ENV{SECURE_BOOT_TURNKEY}) && ($ENV{SECURE_BOOT_TURNKEY} eq "y") ) {
if ( $ENV{SECURE_BOOT_TK_MODE_REQ} eq "FLD" ) {
shell(
"$ENV{HOSTTOOLS_DIR}/SecureBootUtils/genkeyst $ENV{KEYSTORE_ARGS} --args keyinfo=$ENV{WDIR}/keyinf \\
--args ek=$ENV{WDIR}/FLD_EK.enc --args iv=$ENV{WDIR}/FLD_IV.enc --args hash=$ENV{TK_FLD_HASH} \\
--args mid=$ENV{SECURE_BOOT_TK_MID}"
);
}
}
}
print "$0 Executed $args[0] stage\n";
exit 0;
}
# Building
# Non-secure and/or FLd secure image
#
# This demonstrates how to finalize an image. Here, one would insert any additional components to be included
# in the signature block for the boot filesystem
# Create the block of hashes
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/mkhashes \\
--out=$ENV{WDIR}/hashes.bin --item rootfs=$ENV{PROFILE_DIR}/squashfs.img --file cferam.000 --boot $ENV{WDIR}/bootfs"
);
#
# Get rid of the non-hash signature
shell("rm -f $ENV{WDIR}/bootfs/vmlinux.sig");
#
#
#
# build UBI cferom
#
# start with a 1meg empty region
shell("$ENV{HOSTTOOLS_DIR}/imagetools/gen1meg $ENV{WDIR}/region");
#
# and put a copy of cferom in flash with nonsecure headers
foreach (@unsec_offset) {
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/insertboot --arch $ENV{SECURE_BOOT_ARCH} --cfe $CFE_ROM --nonsec \\
--chip=$ENV{BRCM_CHIP} --offset $_ $ENV{WDIR}/region"
);
}
if ( $ENV{BUILD_SECURE_BOOT} eq 'y' ) {
# For every openssl call in this script there is an opportunity to replace it with a call to a remote server, HSM or web portal
# In such case it is assumed that header hashes or will be sent and returned sign to resume image assemble
#
# Below is a local openssl implementaion
#
shell(
"cat $ENV{WDIR}/.hashes.mfg.sig $ENV{WDIR}/.hashes.mfg > $ENV{WDIR}/hashes.mfg"
);
$offset += $image_alloc_size;
#
# insert 1 copy of the MFG or FLD signed cferom into flash image
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/insertboot --arch $ENV{SECURE_BOOT_ARCH} --cfe $CFE_ROM$ENV{MFG_ENC} --mfg=$ENV{WDIR}/.auth.header.mfg.sig \\
--cred=$ENV{MFG_CRED_LIST} --chip=$ENV{BRCM_CHIP} --offset $sec_offset[0] $ENV{WDIR}/region"
);
shell(
"cat $ENV{WDIR}/.hashes.fld.sig $ENV{WDIR}/.hashes.fld > $ENV{WDIR}/hashes.fld"
);
#
# insert 1 copy of the FLD signed cferom into flash image
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/insertboot --arch $ENV{SECURE_BOOT_ARCH} --cfe $ENV{WDIR}/$ENV{CFE_ROM_BN}$ENV{FLD_ENC} --field=$ENV{WDIR}/.auth.header.fld.sig \\
--cred=$ENV{FLD_CRED_LIST} --chip=$ENV{BRCM_CHIP} --offset $sec_offset[1] $ENV{WDIR}/region"
);
shell("cp -f $ENV{WDIR}/hashes.fld $ENV{WDIR}/hashes.mfg $ENV{WDIR}/bootfs");
# cleanup
#shell(
# "rm -f $ENV{WDIR}/.hashes.fld $ENV{WDIR}/.hashes.mfg $ENV{WDIR}/.hashes.mfg.sig $ENV{WDIR}/.hashes.fld.sig $ENV{WDIR}/auth_header $ENV{WDIR}/auth_header.sig"
#);
}
# insert keystore if configured
#
if ( $ENV{SECURE_BOOT_TURNKEY} eq "y" ) {
if ( $ENV{SECURE_BOOT_TK_MODE_REQ} eq "MFG" ) {
shell(
"$ENV{HOSTTOOLS_DIR}/SecureBootUtils/genkeyst $ENV{KEYSTORE_ARGS} --args out=$ENV{WDIR}/keyst.bin"
);
}
elsif ( $ENV{SECURE_BOOT_TK_MODE_REQ} eq "FLD" ) {
# At this stage an outside script has encrypted fld keys and had signed keyinfo
shell("cat $ENV{WDIR}/keyinf.sig $ENV{WDIR}/keyinf > $ENV{WDIR}/keyinf.signed");
shell(
"$ENV{HOSTTOOLS_DIR}/SecureBootUtils/genkeyst $ENV{KEYSTORE_ARGS} --args keystore=$ENV{WDIR}/keyinf.signed --args out=$ENV{WDIR}/keyst.bin"
);
}
#append to boot region @ the offset
shell(
"$ENV{HOSTTOOLS_DIR}/SecureBootUtils/utils -optn update=$ENV{WDIR}/region -optn offs=$ENV{SECURE_BOOT_TK_KS_OFFS}K -optn in=$ENV{WDIR}/keyst.bin"
);
#shell(
# "rm -f $ENV{WDIR}/keyinf.* $ENV{WDIR}/keyst.bin $ENV{WDIR}/FLD_IV.enc $ENV{WDIR}/FLD_EK.enc"
#);
}
#
# Copy hashes for Non-Secure, MFG and FLD to bootfs
shell("cp -f $ENV{WDIR}/hashes.bin $ENV{WDIR}/hashes.mfg $ENV{WDIR}/hashes.fld $ENV{WDIR}/bootfs");
#
# Create the pureubi "blob.bin" file with cferam, vmlinux, dtbs, etc...
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/mkfs.nada --out=$ENV{WDIR}/head/blob.bin $ENV{WDIR}/bootfs"
);
# Then create the metadata for the image
shell(
"$ENV{HOSTTOOLS_DIR}/imagetools/mkfs.nada --out=$ENV{WDIR}/head/meta.bin --extra cferam.000=998 --extra squash=1 --extra committed=0"
);
#
# dump out a binary containing default NVRAM contents
#
#
shell("$ENV{HOSTTOOLS_DIR}/createimg.pl --set boardid=$ENV{BRCM_BOARD_ID} voiceboardid=$ENV{BRCM_VOICE_BOARD_ID} \\
numbermac=$ENV{BRCM_NUM_MAC_ADDRESSES} macaddr=$ENV{BRCM_BASE_MAC_ADDRESS} tp=$ENV{BRCM_MAIN_TP_NUM} \\
psisize=$ENV{BRCM_PSI_SIZE} logsize=$ENV{BRCM_LOG_SECTION_SIZE} auxfsprcnt=$ENV{BRCM_AUXFS_PERCENT} \\
gponsn=$ENV{BRCM_GPON_SERIAL_NUMBER} gponpw=$ENV{BRCM_GPON_PASSWORD} --nvramfile $ENV{HOSTTOOLS_DIR}/nvram.h \\
--nvramdefsfile $ENV{HOSTTOOLS_DIR}/nvram_defaults.h --config=$ENV{HOSTTOOLS_DIR}/local_install/conf/$ENV{TOOLCHAIN_PREFIX}.conf \\
--outputfile=$ENV{WDIR}/$ENV{BRCM_BOARD_ID}_nvram.bin --output_nvram_bin_only");
#
#build_ubi
#
my $BRCM_CHIP = (!defined( $ENV{TAG_OVERRIDE}))?$ENV{BRCM_CHIP} : die "Chip is undefined";
# create tagged-image-format based eMMC image
# Remove leading offset space from cferom region ( we need this for cfe_fs_kernel image format )
# Note that this offset should match the offset of the 1st CFEROM image from the start of flash
#tail -c +$$((65536+1)) $ENV{WDIR}/region > $ENV{WDIR}/region_nooffset;
shell("dd if=$ENV{WDIR}/region skip=65536 bs=1 of=$ENV{WDIR}/region_nooffset");
#
# Create emmc fs_kernel image
#
shell("$ENV{HOSTTOOLS_DIR}/bcmImageBuilder $ENV{BRCM_ENDIAN_FLAGS} \\
--output $ENV{WDIR}/custom_$ENV{FS_KERNEL_IMAGE_NAME}_emmc_squashfs --chip $BRCM_CHIP \\
--board $ENV{BRCM_BOARD_ID} --blocksize 2048 --image-version $ENV{IMAGE_VERSION}_blah --cfefile $ENV{WDIR}/region_nooffset \\
--rootfsfile $ENV{PROFILE_DIR}/squashfs.img --bootfsfile $ENV{WDIR}/head/blob.bin --mdatafile $ENV{WDIR}/head/meta.bin ");
#
# Create emmc cfe_fs_kernel image
#
shell("$ENV{HOSTTOOLS_DIR}/bcmImageBuilder $ENV{BRCM_ENDIAN_FLAGS} --output \\
$ENV{WDIR}/custom_$ENV{CFE_FS_KERNEL_IMAGE_NAME}_emmc_squashfs --chip $BRCM_CHIP \\
--board $ENV{BRCM_BOARD_ID} --blocksize 2048 --image-version $ENV{IMAGE_VERSION}_blah --cfefile $ENV{WDIR}/region_nooffset \\
--rootfsfile $ENV{PROFILE_DIR}/squashfs.img --bootfsfile $ENV{WDIR}/head/blob.bin --mdatafile $ENV{WDIR}/head/meta.bin --include-cfe");
#
# Generate whole flash image for eMMC Boot and Data partition
#
shell("$ENV{HOSTTOOLS_DIR}/create_emmc_rawimg.py --rootfs_file $ENV{PROFILE_DIR}/squashfs.img \\
--bootfs_file $ENV{WDIR}/head/blob.bin --mdata_file $ENV{WDIR}/head/meta.bin \\
--nvram_file $ENV{WDIR}/$ENV{BRCM_BOARD_ID}_nvram.bin --data_sizeMB 10 \\
--rawfullimg_file $ENV{WDIR}/custom_$ENV{FLASH_IMAGE_NAME_EMMC_DATA_PHYSPART}_squashfs.w \\
--cferom_file $ENV{WDIR}/region --cferom_offsetkB 0 --rawcfeimg_file $ENV{WDIR}/custom_$ENV{FLASH_IMAGE_NAME_EMMC_BOOT_PHYSPART}.w \\
--emmcdefs_file $ENV{INC_BRCMSHARED_PUB_PATH}/$ENV{BRCM_BOARD}/emmc_base_defs.h");
shell("mv $ENV{WDIR}/bootfs $ENV{WDIR}/bootfs.$$");
shell("mv $ENV{WDIR}/head $ENV{WDIR}/head.$$");
print "$0 completed \n";