Skip to content

Commit 6d0a176

Browse files
committed
Create u-boot.bin.hardkernel that can be booted from both SD and eMMC
SD: bl1 uses a 512 offset and expects header+checksum at 528-623 and bl2 at 4608+ eMMC: bl1 expects header+checksum at 16-111 and bl2 at 4096+ u-boot.bin.hardkernel layout: 16-111: eMMC header+checksum (start addr set to 1024) 528-623: SD header+checksum 1024-1315: code that copies bl2 from 4608 to 4096 4608-49151: bl2+bl21 49152+: bl30+bl301, bl31 and bl33(u-boot) bl2.bin is modified to remove +512 image offset used when sd-booting by replacing 934: 91080273 add x19, x19, #0x200 // src += 512; with 934: d503201f nop aml_chksum is a small tool that writes new headers with updated offsets and checksums Write to SD/eMMC with: dd if=fip/u-boot.bin.hardkernel of=/dev/mmcblk0 conv=fsync bs=1 count=112 dd if=fip/u-boot.bin.hardkernel of=/dev/mmcblk0 conv=fsync bs=512 seek=1 skip=1
1 parent eb87684 commit 6d0a176

7 files changed

Lines changed: 579 additions & 2 deletions

File tree

‎Makefile‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,11 @@ endif
873873
endif
874874
FIP_ARGS += --bl33 $(FIP_FOLDER_SOC)/bl33.bin
875875

876+
.PHONY: aml_chksum
877+
aml_chksum:
878+
$(Q)$(MAKE) -C $(srctree)/tools/aml_chksum
879+
$(Q)cp $(srctree)/tools/aml_chksum/aml_chksum $(FIP_FOLDER_SOC)/
880+
876881
.PHONY: fip.bin
877882
ifeq ($(CONFIG_NEED_BL301), y)
878883
fip.bin: tools prepare acs.bin bl301.bin
@@ -910,11 +915,16 @@ bl21.bin: tools prepare u-boot.bin acs.bin
910915
$(Q)cp $(buildtree)/board/${BOARDDIR}/firmware/bl21.bin $(FIP_FOLDER_SOC)/bl21.bin -f
911916

912917
.PHONY : boot.bin
913-
boot.bin: fip.bin
918+
boot.bin: fip.bin aml_chksum
914919
ifeq ($(CONFIG_AML_UBOOT_AUTO_TEST), y)
915920
$(Q)python $(FIP_FOLDER)/acs_tool.pyc $(FIP_FOLDER_SOC)/bl2_utst.bin $(FIP_FOLDER_SOC)/bl2_acs.bin $(FIP_FOLDER_SOC)/acs.bin 0
916921
else
917-
$(Q)python $(FIP_FOLDER)/acs_tool.pyc $(FIP_FOLDER_SOC)/bl2.bin $(FIP_FOLDER_SOC)/bl2_acs.bin $(FIP_FOLDER_SOC)/acs.bin 0
922+
# Remove +512 image offset used for sd-card by replacing
923+
# 934: 91080273 add x19, x19, #0x200 // src += 512;
924+
# with
925+
# 934: d503201f nop
926+
$(Q)sed 's/\x73\x02\x08\x91/\x1F\x20\x03\xD5/' $(FIP_FOLDER_SOC)/bl2.bin > $(FIP_FOLDER_SOC)/bl2_nop.bin
927+
$(Q)python $(FIP_FOLDER)/acs_tool.pyc $(FIP_FOLDER_SOC)/bl2_nop.bin $(FIP_FOLDER_SOC)/bl2_acs.bin $(FIP_FOLDER_SOC)/acs.bin 0
918928
endif
919929
$(Q)$(FIP_FOLDER)/blx_fix.sh \
920930
$(FIP_FOLDER_SOC)/bl2_acs.bin \
@@ -954,6 +964,26 @@ ifeq ($(CONFIG_AML_CRYPTO_IMG), y)
954964
$(Q)$(FIP_FOLDER_SOC)/aml_encrypt_$(SOC) --imgsig --input $(srctree)/board/$(BOARDDIR)/boot.img --amluserkey $(srctree)/board/$(BOARDDIR)/aml-user-key.sig --output $(FIP_FOLDER_SOC)/boot.img.encrypt
955965
@cp -f $(FIP_FOLDER_SOC)/boot.img.encrypt $(FIP_FOLDER)/boot.img.encrypt
956966
endif
967+
# Create u-boot.bin.hardkernel that can be booted from both SD and eMMC
968+
# SD: bl1 uses a 512 offset and expects header+checksum at 528-623 and bl2 at 4608+
969+
# eMMC: bl1 expects header+checksum at 16-111 and bl2 at 4096+
970+
#
971+
# u-boot.bin.hardkernel layout:
972+
# 16-111: eMMC header+checksum (start addr set to 1024)
973+
# 528-623: SD header+checksum
974+
# 1024-1315: code that copies bl2 from 4608 to 4096
975+
# 4608-49151: bl2+bl21
976+
# 49152+: bl30+bl301, bl31 and bl33(u-boot)
977+
#
978+
# Clone u-boot.bin and move bl2 from 4096 to 4608
979+
$(Q)dd if=$(FIP_FOLDER_SOC)/u-boot.bin of=$(FIP_FOLDER_SOC)/u-boot.bin.hardkernel bs=512 conv=fsync
980+
$(Q)dd if=$(FIP_FOLDER_SOC)/u-boot.bin of=$(FIP_FOLDER_SOC)/u-boot.bin.hardkernel bs=512 seek=9 skip=8 count=87 conv=fsync,notrunc
981+
$(Q)dd if=/dev/zero of=$(FIP_FOLDER_SOC)/u-boot.bin.hardkernel bs=512 seek=8 count=1 conv=fsync,notrunc
982+
# Add code that copies bl2 from 4608 to 4096
983+
$(Q)dd if=$(FIP_FOLDER_SOC)/bl1.bin.hardkernel of=$(FIP_FOLDER_SOC)/u-boot.bin.hardkernel bs=512 seek=2 skip=2 count=1 conv=fsync,notrunc
984+
# Write headers with correct offsets and checksums
985+
$(Q)$(FIP_FOLDER_SOC)/aml_chksum $(FIP_FOLDER_SOC)/u-boot.bin.hardkernel
986+
#
957987
@cp -f $(FIP_FOLDER_SOC)/u-boot.* $(FIP_FOLDER)/
958988
@rm -f $(FIP_FOLDER_SOC)/bl2_new.bin $(FIP_FOLDER_SOC)/boot_new.bin
959989
@echo '$(FIP_FOLDER_SOC)/u-boot.bin build done!'
@@ -1474,6 +1504,8 @@ distclean: mrproper
14741504
@rm -f $(FIP_FOLDER_SOC)/u-boot.bin.* $(FIP_FOLDER_SOC)/*.encrypt
14751505
@rm -f $(FIP_FOLDER)/u-boot.bin.* $(FIP_FOLDER)/*.bin $(FIP_FOLDER)/*.encrypt
14761506
@rm -f $(srctree)/fip/aml_encrypt_gxb
1507+
@rm -f $(FIP_FOLDER_SOC)/aml_chksum
1508+
@$(MAKE) -C $(srctree)/tools/aml_chksum clean
14771509

14781510
backup:
14791511
F=`basename $(srctree)` ; cd .. ; \

‎fip/gxb/bl1.bin.hardkernel‎

48.5 KB
Binary file not shown.

‎tools/aml_chksum/Makefile‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
PROJECT = aml_chksum
3+
OBJECTS = aml_chksum.o sha256.o
4+
5+
CFLAGS = -Wall -Werror -pedantic -std=c99
6+
ifeq (${DEBUG},1)
7+
CFLAGS += -g -O0 -DDEBUG
8+
else
9+
CFLAGS += -O2
10+
endif
11+
12+
# Make soft links and include from local directory otherwise wrong headers
13+
# could get pulled in from firmware tree.
14+
INCLUDE_PATHS = -I.
15+
16+
CC := gcc
17+
RM := rm -rf
18+
19+
.PHONY: all clean
20+
21+
all: ${PROJECT}
22+
23+
${PROJECT}: ${OBJECTS} Makefile
24+
@echo " LD $@"
25+
${Q}${CC} ${OBJECTS} -o $@
26+
27+
%.o: %.c %.h Makefile
28+
@echo " CC $<"
29+
${Q}${CC} -c ${CFLAGS} ${INCLUDE_PATHS} $< -o $@
30+
31+
clean:
32+
${Q}${RM} ${PROJECT}
33+
${Q}${RM} ${OBJECTS}

‎tools/aml_chksum/aml_chksum.c‎

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-2.0+
3+
*/
4+
5+
#include <errno.h>
6+
#include <stdlib.h>
7+
#include <stdio.h>
8+
#include <stdint.h>
9+
#include <string.h>
10+
11+
#include "aml_chksum.h"
12+
#include "sha256.h"
13+
14+
static void printf_sha256_sum(const char *format, const uint8_t *sum)
15+
{
16+
int index = 0;
17+
char str[SHA256_SUM_LEN * 2 + 1];
18+
19+
str[0] = '\0';
20+
for (index = 0; index < SHA256_SUM_LEN; ++index) {
21+
sprintf(str, "%s%02x", str, sum[index]);
22+
}
23+
printf(format, str);
24+
}
25+
26+
int main(int argc, char **argv)
27+
{
28+
// aml_chksum takes one argument with the path to a u-boot.bin
29+
if (argc < 2) {
30+
printf("ERROR: Missing filename.\n");
31+
return EINVAL;
32+
}
33+
char *filename = argv[1];
34+
35+
FILE *fp = fopen(filename, "r+b");
36+
if (fp == NULL) {
37+
printf("Error: Cannot open file \"%s\": %s\n", filename, strerror(errno));
38+
return errno;
39+
}
40+
41+
// read header data from 0
42+
uint8_t *header_buffer = calloc(AML_HEADER_READ_SIZE, sizeof(uint8_t));
43+
if (fseek(fp, 0, SEEK_SET) != 0) {
44+
printf("ERROR: Cannot seek to header: %s\n", strerror(errno));
45+
return errno;
46+
}
47+
int read_header_size = fread(header_buffer, sizeof(uint8_t), AML_HEADER_READ_SIZE, fp);
48+
if (read_header_size != AML_HEADER_READ_SIZE) {
49+
printf("ERROR: Cannot read header: %s\n", strerror(errno));
50+
return errno;
51+
}
52+
53+
// basic header check
54+
st_aml_block_header *header = (st_aml_block_header *)(header_buffer + AML_HEADER_OFFSET);
55+
if (header->dwMagic != AML_BLK_ID ||
56+
header->byVerMajor != AML_BLK_VER_MJR ||
57+
header->byVerMinor != AML_BLK_VER_MIN ||
58+
header->bySizeHdr != AML_HEADER_SIZE ||
59+
header->nSigType != 0 ||
60+
header->nPUKType != 0) {
61+
printf("ERROR: Bad header.\n");
62+
return EINVAL;
63+
}
64+
65+
// sha256 sum is the 32b following the header
66+
uint8_t *origSum = (header_buffer + AML_HEADER_OFFSET + AML_HEADER_SIZE);
67+
68+
printf("nTotalSize=%u\n", header->nTotalSize);
69+
printf("nCHKStart=%u\n", header->nCHKStart);
70+
printf("nCHKSize=%u\n", header->nCHKSize);
71+
printf("nDataOffset=%u\n", header->nDataOffset);
72+
printf("nDataLen=%u\n", header->nDataLen);
73+
printf_sha256_sum("origSum=%s\n", origSum);
74+
75+
// initialize sha context
76+
uint8_t genSum[SHA256_SUM_LEN];
77+
sha256_context ctx;
78+
sha256_starts(&ctx);
79+
80+
// start with header
81+
sha256_update(&ctx, (uint8_t *)header, AML_HEADER_SIZE);
82+
83+
// read raw data starting from header position + chk start
84+
uint8_t *data_buffer = calloc(header->nCHKSize, sizeof(uint8_t));
85+
if (fseek(fp, AML_HEADER_OFFSET + header->nCHKStart, SEEK_SET) != 0) {
86+
printf("ERROR: Cannot seek to data: %s\n", strerror(errno));
87+
return errno;
88+
}
89+
int read_data_size = fread(data_buffer, sizeof(uint8_t), header->nCHKSize, fp);
90+
if (read_data_size != header->nCHKSize) {
91+
printf("ERROR: Cannot read data: %s\n", strerror(errno));
92+
return errno;
93+
}
94+
95+
// append raw data and finish
96+
sha256_update(&ctx, data_buffer, header->nCHKSize);
97+
sha256_finish(&ctx, genSum);
98+
printf_sha256_sum("genSum=%s\n", genSum);
99+
100+
// clone header
101+
uint8_t *sd_header_buffer = calloc(AML_HEADER_READ_SIZE, sizeof(uint8_t));
102+
memcpy(sd_header_buffer, header_buffer, AML_HEADER_READ_SIZE);
103+
st_aml_block_header *sd_header = (st_aml_block_header *)(sd_header_buffer + AML_HEADER_OFFSET);
104+
uint8_t *sdSum = (sd_header_buffer + AML_HEADER_OFFSET + AML_HEADER_SIZE);
105+
106+
// update sd header
107+
sd_header->nTotalSize -= AML_SD_OFFSET;
108+
sd_header->nDataLen -= AML_SD_OFFSET;
109+
sd_header->nCHKStart = AML_HEADER_SIZE + AML_CHKSUM_SIZE; // start after checksum
110+
sd_header->nCHKSize = sd_header->nTotalSize - sd_header->nCHKStart; // must be less then total size, bl1 only reads ~48kb to memory
111+
printf("nTotalSize=%u\n", sd_header->nTotalSize);
112+
printf("nCHKStart=%u\n", sd_header->nCHKStart);
113+
printf("nCHKSize=%u\n", sd_header->nCHKSize);
114+
printf("nDataOffset=%u\n", sd_header->nDataOffset);
115+
printf("nDataLen=%u\n", sd_header->nDataLen);
116+
117+
// read raw data starting from sd header position + chk start
118+
free(data_buffer);
119+
data_buffer = calloc(sd_header->nCHKSize, sizeof(uint8_t));
120+
if (fseek(fp, AML_SD_OFFSET + AML_HEADER_OFFSET + sd_header->nCHKStart, SEEK_SET) != 0) {
121+
printf("ERROR: Cannot seek to data: %s\n", strerror(errno));
122+
return errno;
123+
}
124+
read_data_size = fread(data_buffer, sizeof(uint8_t), sd_header->nCHKSize, fp);
125+
if (read_data_size != sd_header->nCHKSize) {
126+
printf("ERROR: Cannot read data: %s\n", strerror(errno));
127+
return errno;
128+
}
129+
130+
// calculate checksum for sd header
131+
sha256_starts(&ctx);
132+
sha256_update(&ctx, (uint8_t *)sd_header, AML_HEADER_SIZE);
133+
sha256_update(&ctx, data_buffer, sd_header->nCHKSize);
134+
sha256_finish(&ctx, sdSum);
135+
printf_sha256_sum("sdSum=%s\n", sdSum);
136+
137+
// write sd header to 512
138+
if (fseek(fp, AML_SD_OFFSET, SEEK_SET) != 0) {
139+
printf("ERROR: Cannot seek to sd header: %s\n", strerror(errno));
140+
return errno;
141+
}
142+
int write_size = fwrite(sd_header_buffer, sizeof(uint8_t), AML_HEADER_READ_SIZE, fp);
143+
if (write_size != AML_HEADER_READ_SIZE) {
144+
printf("ERROR: Cannot write sd header: %s\n", strerror(errno));
145+
return errno;
146+
}
147+
fflush(fp);
148+
149+
// new header
150+
header->nCHKStart = AML_SD_OFFSET - AML_HEADER_OFFSET; // must be 496+ for MBR support
151+
header->nCHKSize = header->nTotalSize - header->nCHKStart; // must be less then total size, bl1 only reads ~48kb to memory
152+
header->nDataOffset = 1024 - 16; // point to code that copies bl2 from 4608 to 4096
153+
header->nDataLen = header->nTotalSize - header->nDataOffset;
154+
printf("nCHKStart=%u\n", header->nCHKStart);
155+
printf("nCHKSize=%u\n", header->nCHKSize);
156+
printf("nDataOffset=%u\n", header->nDataOffset);
157+
printf("nDataLen=%u\n", header->nDataLen);
158+
159+
// read raw data starting from new header position + chk start
160+
free(data_buffer);
161+
data_buffer = calloc(header->nCHKSize, sizeof(uint8_t));
162+
if (fseek(fp, AML_HEADER_OFFSET + header->nCHKStart, SEEK_SET) != 0) {
163+
printf("ERROR: Cannot seek to data: %s\n", strerror(errno));
164+
return errno;
165+
}
166+
read_data_size = fread(data_buffer, sizeof(uint8_t), header->nCHKSize, fp);
167+
if (read_data_size != header->nCHKSize) {
168+
printf("ERROR: Cannot read data: %s\n", strerror(errno));
169+
return errno;
170+
}
171+
172+
// calculate checksum for new header
173+
sha256_starts(&ctx);
174+
sha256_update(&ctx, (uint8_t *)header, AML_HEADER_SIZE);
175+
sha256_update(&ctx, data_buffer, header->nCHKSize);
176+
sha256_finish(&ctx, origSum);
177+
printf_sha256_sum("newSum=%s\n", origSum);
178+
179+
// write new header to 0
180+
if (fseek(fp, 0, SEEK_SET) != 0) {
181+
printf("ERROR: Cannot seek to header: %s\n", strerror(errno));
182+
return errno;
183+
}
184+
write_size = fwrite(header_buffer, sizeof(uint8_t), AML_HEADER_READ_SIZE, fp);
185+
if (write_size != AML_HEADER_READ_SIZE) {
186+
printf("ERROR: Cannot write header: %s\n", strerror(errno));
187+
return errno;
188+
}
189+
fflush(fp);
190+
191+
fclose(fp);
192+
free(data_buffer);
193+
free(header_buffer);
194+
free(sd_header_buffer);
195+
return 0;
196+
}

‎tools/aml_chksum/aml_chksum.h‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#ifndef _AML_CHKSUM_H
2+
#define _AML_CHKSUM_H
3+
4+
#define AML_SD_OFFSET (512)
5+
#define AML_HEADER_OFFSET (16)
6+
#define AML_HEADER_SIZE (64)
7+
#define AML_CHKSUM_SIZE (32)
8+
#define AML_HEADER_READ_SIZE (AML_HEADER_OFFSET + AML_HEADER_SIZE + AML_CHKSUM_SIZE)
9+
10+
// Below defines and struct is copied from arch/arm/cpu/armv8/common/firmware/plat/gxb/crypto/secureboot.c
11+
// before bl2 code was removed in "ae6230b5aff5d02f03198d4f6a22e32ae2502447: Remove bl2 code"
12+
13+
#define AML_BLK_ID (0x4C4D4140)
14+
#define AML_BLK_VER_MJR (1)
15+
#define AML_BLK_VER_MIN (0)
16+
17+
typedef struct __st_aml_block_header{
18+
//16
19+
unsigned int dwMagic; //"@AML"
20+
unsigned int nTotalSize; //total size: sizeof(hdr)+
21+
// nSigLen + nDataLen
22+
unsigned char bySizeHdr; //sizeof(st_aml_block)
23+
unsigned char byRootKeyIndex;//root key index; only romcode
24+
// will use it, others just skip
25+
unsigned char byVerMajor; //major version
26+
unsigned char byVerMinor; //minor version
27+
28+
unsigned char szPadding1[4]; //padding???
29+
30+
//16+16
31+
unsigned int nSigType; //e_aml_sig_type : AML_SIG_TYPE_NONE...
32+
unsigned int nSigOffset; //sig data offset, include header
33+
unsigned int nSigLen; //sig data length
34+
//unsigned char szPadding2[4]; //padding???
35+
unsigned int nCHKStart; //begin to be protected with SHA2
36+
37+
//32+16
38+
unsigned int nPUKType; //e_aml_data_type : AML_DATA_TYPE_PROGRAM
39+
unsigned int nPUKOffset; //raw data offset, include header
40+
unsigned int nPUKDataLen; //raw data length
41+
//unsigned char szPadding4[4]; //padding???
42+
unsigned int nCHKSize; //size to be protected with SHA2
43+
44+
//48+16
45+
unsigned int nDataType; //e_aml_data_type : AML_DATA_TYPE_PROGRAM
46+
unsigned int nDataOffset; //raw data offset, include header
47+
unsigned int nDataLen; //raw data length
48+
unsigned char szPadding3[4]; //padding???
49+
50+
//64
51+
} st_aml_block_header;
52+
53+
#endif /* _AML_CHKSUM_H */

0 commit comments

Comments
 (0)