[U-Boot] [PATCH] imx: spl: inject 'u-boot, spl-boot-device' for next-stage
This implements the 'spl_perform_fixups' hook for i.MX-based boards and injects the /chosen/u-boot,spl-boot-device with a string representation corresponding to the boot device used. The intended usage is for the full U-Boot stage to evaluate this in scripts and then adapt its boot-order as needed. This change is heavily based on commit e5f2ecc75001 ("rockchip: rk3399: inject 'u-boot, spl-boot-device' for next-stage") A string representation of the boot device was chosen here (as opposed to an ofpath in the rockchip commit), so as to make this less hardware dependent. Signed-off-by: André Draszik Cc: Stefano Babic Cc: Fabio Estevam Cc: "NXP i.MX U-Boot Team" Cc: Albert Aribaud --- arch/arm/mach-imx/spl.c | 49 + 1 file changed, 49 insertions(+) diff --git a/arch/arm/mach-imx/spl.c b/arch/arm/mach-imx/spl.c index f025c4b301..42ca719cd8 100644 --- a/arch/arm/mach-imx/spl.c +++ b/arch/arm/mach-imx/spl.c @@ -176,6 +176,55 @@ u32 spl_boot_device(void) } #endif /* CONFIG_MX7 || CONFIG_IMX8M || CONFIG_IMX8 */ +static const char *imx_decode_boot_device(u32 boot_device) +{ + int i; + static const struct { + u32 boot_device; + const char *ofpath; + } spl_boot_devices_tbl[] = { + { BOOT_DEVICE_MMC1, "mmc1" }, + { BOOT_DEVICE_MMC2, "mmc2" }, + { BOOT_DEVICE_SPI, "spi" }, + { BOOT_DEVICE_NAND, "nand" }, + }; + + for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i) + if (spl_boot_devices_tbl[i].boot_device == boot_device) + return spl_boot_devices_tbl[i].ofpath; + + return NULL; +} + +void spl_perform_fixups(struct spl_image_info *spl_image) +{ + void *blob = spl_image->fdt_addr; + const char *boot_ofpath; + int chosen; + + /* +* Inject the ofpath of the device the full U-Boot (or Linux in +* Falcon-mode) was booted from into the FDT, if a FDT has been +* loaded at the same time. +*/ + if (!blob) + return; + boot_ofpath = imx_decode_boot_device(spl_image->boot_device); + if (!boot_ofpath) { + pr_err("%s: could not map boot_device to ofpath\n", __func__); + return; + } + + chosen = fdt_find_or_add_subnode(blob, 0, "chosen"); + if (chosen < 0) { + pr_err("%s: could not find/create '/chosen'\n", __func__); + return; + } + fdt_setprop_string(blob, chosen, + "u-boot,spl-boot-device", boot_ofpath); +} + + #ifdef CONFIG_SPL_USB_GADGET int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) { -- 2.23.0.rc1 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
Re: [U-Boot] [PATCH v2 1/5] SPL: fix printing of image name
Hi, On Tue, 2017-10-03 at 16:55 +0100, André Draszik wrote: > From: André Draszik > > The maximum length of the name of the image is > obviously not sizeof(), which is just the > length of a pointer, but IH_NMLEN. > > fixes: 62cf11c0921a90c6bd62344f4bc069668e6c698c > ("SPL: Limit image name print length") > > Signed-off-by: André Draszik I see these patches haven't been merged yet, is there anything wrong? Cheers, Andre' ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH v2 4/5] tpm: add more useful NV storage permission flags
From: André Draszik TPM_NV_PER_PPREAD: physical presence needed for reading TPM_NV_PER_WRITEDEFINE: persistent write lock by writing size 0 TPM_NV_PER_WRITEALL: write in one go Signed-off-by: André Draszik --- include/tpm.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/tpm.h b/include/tpm.h index 2a7528dd48..760d94865c 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -84,9 +84,12 @@ enum tpm_capability_areas { }; #define TPM_NV_PER_GLOBALLOCK (1U << 15) +#define TPM_NV_PER_PPREAD (1U << 16) #define TPM_NV_PER_PPWRITE (1U << 0) #define TPM_NV_PER_READ_STCLEAR(1U << 31) #define TPM_NV_PER_WRITE_STCLEAR (1U << 14) +#define TPM_NV_PER_WRITEDEFINE (1U << 13) +#define TPM_NV_PER_WRITEALL(1U << 12) enum { TPM_PUBEK_SIZE = 256, -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH v2 5/5] tpm: add more missing va_end()
From: André Draszik While commit 36d35345b1f6 ("tpm: add missing va_end") added some missing calls to va_end(), it missed a few places. Signed-off-by: André Draszik --- lib/tpm.c | 5 - 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/tpm.c b/lib/tpm.c index 42a6591f81..f461e639e0 100644 --- a/lib/tpm.c +++ b/lib/tpm.c @@ -92,6 +92,7 @@ int pack_byte_string(uint8_t *str, size_t size, const char *format, ...) break; default: debug("Couldn't recognize format string\n"); + va_end(args); return -1; } @@ -170,8 +171,10 @@ int unpack_byte_string(const uint8_t *str, size_t size, const char *format, ...) return -1; } - if (offset + length > size) + if (offset + length > size) { + va_end(args); return -1; + } switch (*format) { case 'b': -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH v2 1/5] SPL: fix printing of image name
From: André Draszik The maximum length of the name of the image is obviously not sizeof(), which is just the length of a pointer, but IH_NMLEN. fixes: 62cf11c0921a90c6bd62344f4bc069668e6c698c ("SPL: Limit image name print length") Signed-off-by: André Draszik --- common/spl/spl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/spl/spl.c b/common/spl/spl.c index 4afbe97fc1..7c7467ecd2 100644 --- a/common/spl/spl.c +++ b/common/spl/spl.c @@ -154,7 +154,7 @@ int spl_parse_image_header(struct spl_image_info *spl_image, spl_image->os = image_get_os(header); spl_image->name = image_get_name(header); debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n", - (int)sizeof(spl_image->name), spl_image->name, + IH_NMLEN, spl_image->name, spl_image->load_addr, spl_image->size); #else /* LEGACY image not supported */ -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH v2 3/5] tpm: add tpm_get_random()
From: André Draszik Signed-off-by: André Draszik --- include/tpm.h | 12 lib/tpm.c | 43 +++ 2 files changed, 55 insertions(+) diff --git a/include/tpm.h b/include/tpm.h index f88388f353..2a7528dd48 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -651,4 +651,16 @@ uint32_t tpm_flush_specific(uint32_t key_handle, uint32_t resource_type); uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t pubkey_digest[20], uint32_t *handle); #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ + +/** + * Read random bytes from the TPM RNG. The implementation deals with the fact + * that the TPM may legally return fewer bytes than requested by retrying + * until @p count bytes have been received. + * + * @param data output buffer for the random bytes + * @param countsize of output buffer + * @return return code of the operation + */ +uint32_t tpm_get_random(void *data, uint32_t count); + #endif /* __TPM_H */ diff --git a/lib/tpm.c b/lib/tpm.c index 5659fa5e18..42a6591f81 100644 --- a/lib/tpm.c +++ b/lib/tpm.c @@ -1049,3 +1049,46 @@ uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ #endif /* CONFIG_TPM_AUTH_SESSIONS */ + +uint32_t tpm_get_random(void *data, uint32_t count) +{ + const uint8_t command[14] = { + 0x0, 0xc1, /* TPM_TAG */ + 0x0, 0x0, 0x0, 0xe, /* parameter size */ + 0x0, 0x0, 0x0, 0x46,/* TPM_COMMAND_CODE */ + }; + const size_t length_offset = 10; + const size_t data_size_offset = 10; + const size_t data_offset = 14; + uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; + size_t response_length = sizeof(response); + uint32_t data_size; + uint8_t *out = data; + + while (count > 0) { + uint32_t this_bytes = min(count, + sizeof (response) - data_offset); + uint32_t err; + + if (pack_byte_string(buf, sizeof(buf), "sd", +0, command, sizeof(command), +length_offset, this_bytes)) + return TPM_LIB_ERROR; + err = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + if (unpack_byte_string(response, response_length, "d", + data_size_offset, &data_size)) + return TPM_LIB_ERROR; + if (data_size > count) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_length, "s", + data_offset, out, data_size)) + return TPM_LIB_ERROR; + + count -= data_size; + out += data_size; + } + + return 0; +} -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH v2 2/5] tpm: fix reading of permanent flags
From: André Draszik The offset of the permanent flags structure is in a different place in the response compared to what the code is doing, which gives us a completely useless result. Fix by replacing hand-crafted code with generic parser infrastructure. Signed-off-by: André Draszik --- lib/tpm.c | 12 +++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/tpm.c b/lib/tpm.c index d1cf5a8a16..5659fa5e18 100644 --- a/lib/tpm.c +++ b/lib/tpm.c @@ -607,14 +607,24 @@ uint32_t tpm_get_permanent_flags(struct tpm_permanent_flags *pflags) 0x0, 0x0, 0x0, 0x4, /* subcap size */ 0x0, 0x0, 0x1, 0x8, /* subcap value */ }; + const size_t data_size_offset = TPM_HEADER_SIZE; + const size_t data_offset = TPM_HEADER_SIZE + sizeof (uint32_t); uint8_t response[COMMAND_BUFFER_SIZE]; size_t response_length = sizeof(response); uint32_t err; + uint32_t data_size; err = tpm_sendrecv_command(command, response, &response_length); if (err) return err; - memcpy(pflags, response + TPM_HEADER_SIZE, sizeof(*pflags)); + if (unpack_byte_string(response, response_length, "d", + data_size_offset, &data_size)) + return TPM_LIB_ERROR; + if (data_size < sizeof(*pflags)) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_length, "s", + data_offset, pflags, sizeof(*pflags))) + return TPM_LIB_ERROR; return 0; } -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH 3/5] tpm: add tpm_get_random()
From: André Draszik Signed-off-by: André Draszik --- include/tpm.h | 12 lib/tpm.c | 43 +++ 2 files changed, 55 insertions(+) diff --git a/include/tpm.h b/include/tpm.h index f88388f353..2a7528dd48 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -651,4 +651,16 @@ uint32_t tpm_flush_specific(uint32_t key_handle, uint32_t resource_type); uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t pubkey_digest[20], uint32_t *handle); #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ + +/** + * Read random bytes from the TPM RNG. The implementation deals with the fact + * that the TPM may legally return fewer bytes than requested by retrying + * until @p count bytes have been received. + * + * @param data output buffer for the random bytes + * @param countsize of output buffer + * @return return code of the operation + */ +uint32_t tpm_get_random(void *data, uint32_t count); + #endif /* __TPM_H */ diff --git a/lib/tpm.c b/lib/tpm.c index 5659fa5e18..42a6591f81 100644 --- a/lib/tpm.c +++ b/lib/tpm.c @@ -1049,3 +1049,46 @@ uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ #endif /* CONFIG_TPM_AUTH_SESSIONS */ + +uint32_t tpm_get_random(void *data, uint32_t count) +{ + const uint8_t command[14] = { + 0x0, 0xc1, /* TPM_TAG */ + 0x0, 0x0, 0x0, 0xe, /* parameter size */ + 0x0, 0x0, 0x0, 0x46,/* TPM_COMMAND_CODE */ + }; + const size_t length_offset = 10; + const size_t data_size_offset = 10; + const size_t data_offset = 14; + uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; + size_t response_length = sizeof(response); + uint32_t data_size; + uint8_t *out = data; + + while (count > 0) { + uint32_t this_bytes = min(count, + sizeof (response) - data_offset); + uint32_t err; + + if (pack_byte_string(buf, sizeof(buf), "sd", +0, command, sizeof(command), +length_offset, this_bytes)) + return TPM_LIB_ERROR; + err = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + if (unpack_byte_string(response, response_length, "d", + data_size_offset, &data_size)) + return TPM_LIB_ERROR; + if (data_size > count) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_length, "s", + data_offset, out, data_size)) + return TPM_LIB_ERROR; + + count -= data_size; + out += data_size; + } + + return 0; +} -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH 5/5] tpm: add more missing va_end()
From: André Draszik While commit 36d35345b1f6 ("tpm: add missing va_end") added some missing calls to va_end(), it missed a few places. Signed-off-by: André Draszik --- lib/tpm.c | 5 - 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/tpm.c b/lib/tpm.c index 42a6591f81..f461e639e0 100644 --- a/lib/tpm.c +++ b/lib/tpm.c @@ -92,6 +92,7 @@ int pack_byte_string(uint8_t *str, size_t size, const char *format, ...) break; default: debug("Couldn't recognize format string\n"); + va_end(args); return -1; } @@ -170,8 +171,10 @@ int unpack_byte_string(const uint8_t *str, size_t size, const char *format, ...) return -1; } - if (offset + length > size) + if (offset + length > size) { + va_end(args); return -1; + } switch (*format) { case 'b': -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH 4/5] tpm: add more useful NV storage permission flags
From: André Draszik TPM_NV_PER_PPREAD: physical presence needed for reading TPM_NV_PER_WRITEDEFINE: persistent write lock by writing size 0 TPM_NV_PER_WRITEALL: write in one go Signed-off-by: André Draszik --- include/tpm.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/tpm.h b/include/tpm.h index 2a7528dd48..760d94865c 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -84,9 +84,12 @@ enum tpm_capability_areas { }; #define TPM_NV_PER_GLOBALLOCK (1U << 15) +#define TPM_NV_PER_PPREAD (1U << 16) #define TPM_NV_PER_PPWRITE (1U << 0) #define TPM_NV_PER_READ_STCLEAR(1U << 31) #define TPM_NV_PER_WRITE_STCLEAR (1U << 14) +#define TPM_NV_PER_WRITEDEFINE (1U << 13) +#define TPM_NV_PER_WRITEALL(1U << 12) enum { TPM_PUBEK_SIZE = 256, -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH 2/5] tpm: fix reading of permanent flags
From: André Draszik The offset of the permanent flags structure is in a different place in the response compared to what the code is doing, which gives us a completely result. Fix by replacing hand-crafted code with generic parser infrastructure. Signed-off-by: André Draszik --- lib/tpm.c | 12 +++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/tpm.c b/lib/tpm.c index d1cf5a8a16..5659fa5e18 100644 --- a/lib/tpm.c +++ b/lib/tpm.c @@ -607,14 +607,24 @@ uint32_t tpm_get_permanent_flags(struct tpm_permanent_flags *pflags) 0x0, 0x0, 0x0, 0x4, /* subcap size */ 0x0, 0x0, 0x1, 0x8, /* subcap value */ }; + const size_t data_size_offset = TPM_HEADER_SIZE; + const size_t data_offset = TPM_HEADER_SIZE + sizeof (uint32_t); uint8_t response[COMMAND_BUFFER_SIZE]; size_t response_length = sizeof(response); uint32_t err; + uint32_t data_size; err = tpm_sendrecv_command(command, response, &response_length); if (err) return err; - memcpy(pflags, response + TPM_HEADER_SIZE, sizeof(*pflags)); + if (unpack_byte_string(response, response_length, "d", + data_size_offset, &data_size)) + return TPM_LIB_ERROR; + if (data_size < sizeof(*pflags)) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_length, "s", + data_offset, pflags, sizeof(*pflags))) + return TPM_LIB_ERROR; return 0; } -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot
[U-Boot] [PATCH 1/5] SPL: fix printing of image name
From: André Draszik The maximum length of the name of the image is obviously not sizeof(), which is just the ength of a pointer, but IH_NMLEN. fixes: 62cf11c0921a90c6bd62344f4bc069668e6c698c ("SPL: Limit image name print length") Signed-off-by: André Draszik --- common/spl/spl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/spl/spl.c b/common/spl/spl.c index 4afbe97fc1..7c7467ecd2 100644 --- a/common/spl/spl.c +++ b/common/spl/spl.c @@ -154,7 +154,7 @@ int spl_parse_image_header(struct spl_image_info *spl_image, spl_image->os = image_get_os(header); spl_image->name = image_get_name(header); debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n", - (int)sizeof(spl_image->name), spl_image->name, + IH_NMLEN, spl_image->name, spl_image->load_addr, spl_image->size); #else /* LEGACY image not supported */ -- 2.14.2 ___ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot