Skip to content

Commit 1109194

Browse files
committed
ELF_SCATTERED: added sha check (WIP)
1 parent e7cd340 commit 1109194

7 files changed

Lines changed: 436 additions & 213 deletions

File tree

hal/sim.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,11 @@ void do_boot(const uint32_t *app_offset)
353353
main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff);
354354
main(main_argc, main_argv, NULL, NULL);
355355
#elif defined ELF_SCATTERED
356-
unsigned long *entry_point = (unsigned long *)sim_ram_base;
356+
uint8_t *entry_point = (sim_ram_base + 0x100000);
357+
printf("entry point: %p\n", entry_point);
358+
printf("app offset: %p\n", app_offset);
357359
typedef int (*main_entry)(int, char**);
358360
main_entry main;
359-
360-
wolfBoot_printf("Loading ELF image with scattered segments...\n");
361-
ret = elf_store_image_scattered((void*)app_offset, entry_point, 0);
362-
if (ret != 0) {
363-
wolfBoot_printf( "Error loading ELF image!\n");
364-
exit(-1);
365-
}
366361
main = (main_entry)(entry_point);
367362
main(main_argc, main_argv);
368363
#else

include/elf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ typedef struct elf64_header {
112112
uint16_t sh_str_index;
113113
} elf64_header;
114114

115+
union elf_header {
116+
elf32_header *h32;
117+
elf64_header *h64;
118+
};
119+
115120
typedef struct elf64_section_header {
116121
uint32_t name;
117122
uint32_t type;
@@ -164,6 +169,9 @@ typedef int (*elf_mmu_map_cb)(uint64_t, uint64_t, uint32_t);
164169
int elf_load_image_mmu(uint8_t *image, uintptr_t *entry, elf_mmu_map_cb mmu_cb);
165170
int elf_load_image(uint8_t *image, uintptr_t *entry, int is_ext);
166171
int elf_store_image_scattered(const unsigned char *image, unsigned long *entry_out, int ext_flash);
172+
int elf_check_image_scattered(uint8_t part);
173+
int elf_hdr_size(const unsigned char *ehdr);
174+
int elf_open(const unsigned char *ehdr, int *is_elf32);
167175

168176

169177
#ifdef __cplusplus

include/wolfboot/wolfboot.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,49 @@ extern "C" {
164164

165165
/* Hashing configuration */
166166
#if defined(WOLFBOOT_HASH_SHA256)
167+
# include "wolfssl/wolfcrypt/sha256.h"
167168
# ifndef WOLFBOOT_SHA_BLOCK_SIZE
168169
# define WOLFBOOT_SHA_BLOCK_SIZE (256)
169170
# endif
170171
# define WOLFBOOT_SHA_HDR HDR_SHA256
171172
# define WOLFBOOT_SHA_DIGEST_SIZE (32)
172173
# define image_hash image_sha256
174+
# define header_hash header_sha256
175+
# define update_hash wc_Sha256Update
173176
# define key_hash key_sha256
174177
# define self_hash self_sha256
178+
# define final_hash wc_Sha256Final
179+
typedef wc_Sha256 wolfBoot_hash_t;
180+
# define HDR_HASH HDR_SHA256
175181
#elif defined(WOLFBOOT_HASH_SHA384)
182+
# include "wolfssl/wolfcrypt/sha512.h"
176183
# ifndef WOLFBOOT_SHA_BLOCK_SIZE
177184
# define WOLFBOOT_SHA_BLOCK_SIZE (256)
178185
# endif
179186
# define WOLFBOOT_SHA_HDR HDR_SHA384
180187
# define WOLFBOOT_SHA_DIGEST_SIZE (48)
181188
# define image_hash image_sha384
189+
# define header_hash header_sha384
190+
# define update_hash wc_Sha384Update
182191
# define key_hash key_sha384
183192
# define self_hash self_sha384
193+
# define final_hash wc_Sha384Final
194+
typedef wc_Sha384 wolfBoot_hash_t;
195+
# define HDR_HASH HDR_SHA384
184196
#elif defined(WOLFBOOT_HASH_SHA3_384)
197+
# include "wolfssl/wolfcrypt/sha3.h"
185198
# ifndef WOLFBOOT_SHA_BLOCK_SIZE
186199
# define WOLFBOOT_SHA_BLOCK_SIZE (128)
187200
# endif
188201
# define WOLFBOOT_SHA_HDR HDR_SHA3_384
189202
# define WOLFBOOT_SHA_DIGEST_SIZE (48)
190203
# define image_hash image_sha3_384
204+
# define header_hash header_sha3_384
205+
# define update_hash wc_Sha3Update
206+
# define final_hash wc_Sha3Final
191207
# define key_hash key_sha3_384
208+
typedef wc_sha3_384 wolfBoot_hash_t;
209+
# define HDR_HASH HDR_SHA3_384
192210
#else
193211
# error "No valid hash algorithm defined!"
194212
#endif

src/elf.c

Lines changed: 41 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "hal/nxp_ppc.h"
3434
#endif
3535

36+
#ifdef ELF_SCATTERED
37+
#include "image.h"
38+
#endif
39+
3640
/* support for elf parsing debug printf */
3741
#if defined(DEBUG) || defined(ELF_PARSER)
3842
#if defined(DEBUG_ELF) && DEBUG_ELF == 0
@@ -147,167 +151,52 @@ int elf_load_image_mmu(uint8_t *image, uintptr_t *entry, elf_mmu_map_cb mmu_cb)
147151
}
148152
#endif /* MMU */
149153

150-
#if 0
151-
/**
152-
* @brief Compute the scattered hash by hashing PT_LOAD segments at their XIP
153-
* addresses. Note: This function assumes that the destination addresses for elf
154-
* loading have the same access patterns as the memory represented by img.
155-
* (e.g. if BOOT partition is external, then reads/writes to the load address
156-
* will use ext_flash_read/ext_flash_write.
157-
*
158-
* @param img Pointer to the wolfBoot image
159-
* @param hash Buffer to store the computed hash (must be at least
160-
* WOLFBOOT_SHA_DIGEST_SIZE bytes)
161-
* @return 0 on success, negative value on error
162-
*/
163-
static int wolfBoot_compute_scattered_hash(struct wolfBoot_image *img, uint8_t *hash)
154+
int elf_open(const unsigned char *ehdr, int *is_elf32)
164155
{
165-
uint8_t elf_header_buf[sizeof(elf64_header)];
166-
uint8_t program_header_buf[sizeof(elf64_program_header)];
167-
elf32_header* h32;
168-
elf64_header* h64;
169-
uint16_t entry_count, entry_size;
170-
uint32_t ph_offset;
171-
int is_elf32, is_le, i;
172-
#if defined(WOLFBOOT_HASH_SHA256)
173-
wc_Sha256 sha256_ctx;
174-
#elif defined(WOLFBOOT_HASH_SHA384)
175-
wc_Sha384 sha384_ctx;
176-
#elif defined(WOLFBOOT_HASH_SHA3_384)
177-
wc_Sha3 sha3_384_ctx;
178-
#endif
179-
180-
#ifdef EXT_FLASH
181-
if (PART_IS_EXT(img)) {
182-
/* Read ELF header from external flash */
183-
ext_flash_check_read((uintptr_t)(img->fw_base), elf_header_buf, sizeof(elf64_header));
184-
} else
185-
#endif
186-
{
187-
memcpy(elf_header_buf, (void*)(img->fw_base), sizeof(elf64_header));
188-
}
189-
190-
h32 = (elf32_header*)elf_header_buf;
191-
h64 = (elf64_header*)elf_header_buf;
192-
156+
const unsigned char *ident = ehdr;
193157
/* Verify ELF header */
194-
if (memcmp(h32->ident, ELF_IDENT_STR, 4) != 0) {
158+
if (memcmp(ident, ELF_IDENT_STR, 4) != 0) {
195159
return -1; /* not valid header identifier */
196160
}
161+
wolfBoot_printf("ELF image found\n");
162+
*is_elf32 = !!(ident[ELF_CLASS_OFF] == ELF_CLASS_32);
163+
return 0;
197164

198-
/* Load class and endianess */
199-
is_elf32 = (h32->ident[4] == ELF_CLASS_32);
200-
is_le = (h32->ident[5] == ELF_ENDIAN_LITTLE);
201-
(void)is_le;
202-
203-
/* Initialize hash context */
204-
#if defined(WOLFBOOT_HASH_SHA256)
205-
wc_InitSha256(&sha256_ctx);
206-
#elif defined(WOLFBOOT_HASH_SHA384)
207-
wc_InitSha384(&sha384_ctx);
208-
#elif defined(WOLFBOOT_HASH_SHA3_384)
209-
wc_Sha3_384_Init(&sha3_384_ctx, NULL, INVALID_DEVID);
210-
#endif
211-
212-
/* Get program headers info */
213-
ph_offset = is_elf32 ? GET32(h32->ph_offset) : GET32(h64->ph_offset);
214-
entry_size = is_elf32 ? GET16(h32->ph_entry_size) : GET16(h64->ph_entry_size);
215-
entry_count = is_elf32 ? GET16(h32->ph_entry_count) : GET16(h64->ph_entry_count);
216-
217-
/* Hash each loadable segment directly from its physical address */
218-
for (i = 0; i < entry_count; i++) {
219-
elf32_program_header* phdr32;
220-
elf64_program_header* phdr64;
221-
uint32_t type;
222-
uintptr_t paddr;
223-
uintptr_t file_size;
224-
225-
/* Read program header into buffer */
226-
#ifdef EXT_FLASH
227-
if (PART_IS_EXT(img)) {
228-
ext_flash_check_read((uintptr_t)(img->fw_base) + ph_offset + (i * entry_size),
229-
program_header_buf, entry_size);
230-
} else
231-
#endif
232-
{
233-
memcpy(program_header_buf,
234-
(uint8_t*)(img->fw_base) + ph_offset + (i * entry_size),
235-
entry_size);
236-
}
237-
238-
239-
phdr32 = (elf32_program_header*)program_header_buf;
240-
phdr64 = (elf64_program_header*)program_header_buf;
241-
type = (is_elf32 ? GET32(phdr32->type) : GET32(phdr64->type));
242-
paddr = (is_elf32 ? GET32(phdr32->paddr) : GET64(phdr64->paddr));
243-
file_size = (is_elf32 ? GET32(phdr32->file_size) : GET64(phdr64->file_size));
165+
}
244166

245-
/* Only hash PT_LOAD segments with non-zero size */
246-
if (type == ELF_PT_LOAD && file_size > 0) {
247-
#ifdef DEBUG_ELF
248-
wolfBoot_printf("Hashing segment at %p (%d bytes)\r\n", (void*)paddr, (uint32_t)file_size);
249-
#endif
250-
/* Hash the segment data from physical address in blocks */
251-
uint32_t pos = 0;
252-
while (pos < file_size) {
253-
uint8_t *block;
254-
uint32_t blksz = WOLFBOOT_SHA_BLOCK_SIZE;
255-
256-
if (pos + blksz > file_size) {
257-
blksz = file_size - pos;
258-
}
259-
260-
block = get_sha_block_ptr(img, (const uint8_t *)(paddr + pos));
261-
if (block == NULL) {
262-
return -1;
263-
}
264-
265-
#if defined(WOLFBOOT_HASH_SHA256)
266-
wc_Sha256Update(&sha256_ctx, block, blksz);
267-
#elif defined(WOLFBOOT_HASH_SHA384)
268-
wc_Sha384Update(&sha384_ctx, block, blksz);
269-
#elif defined(WOLFBOOT_HASH_SHA3_384)
270-
wc_Sha3_384_Update(&sha3_384_ctx, block, blksz);
271-
#endif
272-
pos += blksz;
273-
}
274-
}
167+
int elf_hdr_size(const unsigned char *ehdr)
168+
{
169+
int sz = 0;
170+
int is_elf32;
171+
if (elf_open(ehdr, &is_elf32) != 0)
172+
return -1;
173+
if (is_elf32) {
174+
const elf32_header *elf32_hdr = (const elf32_header *)ehdr;
175+
sz = sizeof(elf32_header);
176+
sz += elf32_hdr->ph_entry_count * sizeof(elf32_program_header);
177+
} else {
178+
const elf64_header *elf64_hdr = (const elf64_header *)ehdr;
179+
sz = sizeof(elf64_header);
180+
sz += elf64_hdr->ph_entry_count * sizeof(elf64_program_header);
275181
}
276-
277-
/* Finalize hash */
278-
#if defined(WOLFBOOT_HASH_SHA256)
279-
wc_Sha256Final(&sha256_ctx, hash);
280-
#elif defined(WOLFBOOT_HASH_SHA384)
281-
wc_Sha384Final(&sha384_ctx, hash);
282-
#elif defined(WOLFBOOT_HASH_SHA3_384)
283-
wc_Sha3_384_Final(&sha3_384_ctx, hash);
284-
#endif
285-
286-
return 0;
182+
return sz;
287183
}
288-
#endif
289184

290-
int elf_store_image_scattered(const unsigned char *image, unsigned long *entry_out, int ext_flash) {
291-
const unsigned char *ident;
185+
int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out, int ext_flash) {
186+
const unsigned char *image;
292187
int is_elf32;
293188
unsigned short entry_count;
294189
unsigned short entry_size;
295190
unsigned long entry_off;
296191
int i;
297-
298-
ident = image;
299-
300-
301-
/* Verify ELF header */
302-
if (memcmp(ident, ELF_IDENT_STR, 4) != 0) {
303-
return -1; /* not valid header identifier */
192+
image = hdr + IMAGE_HEADER_SIZE;
193+
if (elf_open(image, &is_elf32) != 0) {
194+
return -1;
304195
}
305-
306-
is_elf32 = (ident[ELF_CLASS_OFF] == ELF_CLASS_32);
307-
308196
if (is_elf32) {
309197
const elf32_header *eh;
310198
const elf32_program_header *ph;
199+
wolfBoot_printf("ELF image is 32 bit\n");
311200

312201
eh = (const elf32_header *)image;
313202
entry_count = eh->ph_entry_count;
@@ -327,12 +216,11 @@ int elf_store_image_scattered(const unsigned char *image, unsigned long *entry_o
327216
paddr = (unsigned long)ph[i].paddr;
328217
offset = (unsigned long)ph[i].offset;
329218
filesz = (unsigned long)ph[i].file_size;
330-
#if 0
331219
#ifdef EXT_FLASH
332220
if (ext_flash) {
333221
ext_flash_unlock();
334-
ext_flash_erase(paddr, filesz);
335-
ext_flash_write(paddr, image + offset, filesz);
222+
ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
223+
ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
336224
ext_flash_lock();
337225
}
338226
else
@@ -343,11 +231,11 @@ int elf_store_image_scattered(const unsigned char *image, unsigned long *entry_o
343231
hal_flash_write(paddr, image + offset, filesz);
344232
hal_flash_lock();
345233
}
346-
#endif
347234
}
348-
} else if (ident[ELF_CLASS_OFF] == ELF_CLASS_64) {
235+
} else { /* 64 bit ELF */
349236
const elf64_header *eh;
350237
const elf64_program_header *ph;
238+
wolfBoot_printf("ELF image is 64 bit\n");
351239

352240
eh = (const elf64_header *)image;
353241
entry_count = eh->ph_entry_count;
@@ -363,33 +251,27 @@ int elf_store_image_scattered(const unsigned char *image, unsigned long *entry_o
363251

364252
if (ph[i].type != ELF_PT_LOAD)
365253
continue;
366-
367254
paddr = (unsigned long)ph[i].paddr;
368255
offset = (unsigned long)ph[i].offset;
369256
filesz = (unsigned long)ph[i].file_size;
370-
#if 0
257+
printf("Writing section at address %lx offset %lx\n", paddr, offset);
371258
#ifdef EXT_FLASH
372259
if (ext_flash) {
373260
ext_flash_unlock();
374-
ext_flash_erase(paddr, filesz);
375-
ext_flash_write(paddr, image + offset, filesz);
261+
ext_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
262+
ext_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
376263
ext_flash_lock();
377264
}
378265
else
379266
#endif
380267
{
381268
hal_flash_unlock();
382-
hal_flash_erase(paddr, filesz);
383-
hal_flash_write(paddr, image + offset, filesz);
269+
hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
270+
hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
384271
hal_flash_lock();
385272
}
386-
#endif
387273
}
388-
} else {
389-
/* Invalid elf header. */
390-
return -1;
391274
}
392-
393275
return 0;
394276
}
395277

0 commit comments

Comments
 (0)