diff options
-rw-r--r-- | cbootimage.h | 3 | ||||
-rw-r--r-- | data_layout.c | 40 | ||||
-rw-r--r-- | set.c | 3 |
3 files changed, 42 insertions, 4 deletions
diff --git a/cbootimage.h b/cbootimage.h index f99c926..1dfa75b 100644 --- a/cbootimage.h +++ b/cbootimage.h @@ -33,12 +33,15 @@ #include <stdlib.h> #include <assert.h> #include <errno.h> +#include <math.h> #define NVBOOT_AES_BLOCK_SIZE_LOG2 4 #define MAX_BUFFER 200 #define MAX_STR_LEN 20 #define MAX_BOOTLOADER_SIZE (16 * 1024 * 1024) #define NVBOOT_BOOTDATA_VERSION(a, b) ((((a)&0xffff) << 16) | ((b)&0xffff)) +#define NVBOOT_BAD_BLOCK_TABLE_SIZE 4096 +#define NV_MAX(a, b) (((a) > (b)) ? (a) : (b)) /* * Enumerations diff --git a/data_layout.c b/data_layout.c index 53e9274..a0b2e11 100644 --- a/data_layout.c +++ b/data_layout.c @@ -80,12 +80,49 @@ static int write_bootloaders(build_image_context *context); static void find_new_journal_blk(build_image_context *context); static int finish_update(build_image_context *context); +static void init_bad_block_table(build_image_context *context); + static u_int32_t iceil_log2(u_int32_t a, u_int32_t b) { return (a + (1 << b) - 1) >> b; } +/* Returns the smallest power of 2 >= a */ +static u_int32_t +ceil_log2(u_int32_t a) +{ + u_int32_t result; + + result = log2(a); + if ((1UL << result) < a) + result++; + + return result; +} + +static void init_bad_block_table(build_image_context *context) +{ + u_int32_t bytes_per_entry; + nvboot_badblock_table *table; + nvboot_config_table *bct; + + bct = (nvboot_config_table *)(context->bct); + + assert(context != NULL); + assert(bct != NULL); + + table = &(bct->badblock_table); + + bytes_per_entry = ICEIL(context->partition_size, + NVBOOT_BAD_BLOCK_TABLE_SIZE); + table->block_size_log2 = context->block_size_log2; + table->virtual_blk_size_log2 = NV_MAX(ceil_log2(bytes_per_entry), + table->block_size_log2); + table->entries_used = iceil_log2(context->partition_size, + table->virtual_blk_size_log2); +} + static block_data *new_block(u_int32_t blk_number, u_int32_t block_size) { block_data *new_block = malloc(sizeof(block_data)); @@ -904,7 +941,8 @@ begin_update(build_image_context *context) pages_per_bct = iceil_log2(bct_size, context->page_size_log2); pages_per_blk = (1 << (context->block_size_log2 - context->page_size_log2)); - + /* Initialize the bad block table field. */ + init_bad_block_table(context); /* Fill the reserved data w/the padding pattern. */ write_padding(context->bct + reserved_offset, reserved_size); @@ -24,7 +24,6 @@ * set.c - State setting support for the cbootimage tool */ -#include <math.h> #include "set.h" #include "cbootimage.h" #include "crypto.h" @@ -40,8 +39,6 @@ * A SetXXX() function may not call any parseing functions. */ -#define NV_MAX(a, b) (((a) > (b)) ? (a) : (b)) - #define CASE_DEVICE_VALUE(prefix, id) \ case token_##id: \ (void)context->bctlib.setdev_param(index, \ |