diff options
author | Winter Wang <wente.wang@nxp.com> | 2016-11-30 18:54:29 +0800 |
---|---|---|
committer | Winter Wang <wente.wang@nxp.com> | 2016-12-08 17:14:57 +0800 |
commit | ba84ac09dd524a2a855be2f3a104f9584f4637e7 (patch) | |
tree | 6908d68ed372dd4328f84a6ff8225cc3eaffd530 | |
parent | 6c5665fe850ad293342e6000827065290a79beba (diff) | |
download | u-boot-imx-ba84ac09dd524a2a855be2f3a104f9584f4637e7.zip u-boot-imx-ba84ac09dd524a2a855be2f3a104f9584f4637e7.tar.gz u-boot-imx-ba84ac09dd524a2a855be2f3a104f9584f4637e7.tar.bz2 |
MA-9077 libavb: fsl: add utils
extract block margin ops from fsl_avb.c to utils
will be used in other modules.
protect it from 0 blksz input.
Change-Id: I5e81774dfb7d7d3377299241616c68ec22ba425c
Signed-off-by: Winter Wang <wente.wang@nxp.com>
-rw-r--r-- | lib/avb/fsl/Makefile | 1 | ||||
-rw-r--r-- | lib/avb/fsl/fsl_avb.c | 73 | ||||
-rw-r--r-- | lib/avb/fsl/utils.c | 51 | ||||
-rw-r--r-- | lib/avb/fsl/utils.h | 25 |
4 files changed, 86 insertions, 64 deletions
diff --git a/lib/avb/fsl/Makefile b/lib/avb/fsl/Makefile index 362c917..6218fab 100644 --- a/lib/avb/fsl/Makefile +++ b/lib/avb/fsl/Makefile @@ -2,3 +2,4 @@ ccflags-$(CONFIG_AVB_DEBUG) += -DAVB_DEBUG obj-y += fsl_avb.o obj-y += fsl_avbkey.o obj-y += fsl_bootctl.o +obj-y += utils.o diff --git a/lib/avb/fsl/fsl_avb.c b/lib/avb/fsl/fsl_avb.c index 6068239..66b5b10 100644 --- a/lib/avb/fsl/fsl_avb.c +++ b/lib/avb/fsl/fsl_avb.c @@ -14,26 +14,11 @@ #include <fsl_avb.h> #include "fsl_avbkey.h" +#include "utils.h" #include "debug.h" #define FSL_AVB_DEV "mmc" -#define ALIGN_BYTES 64 /*mmc block read/write need 64 bytes aligned */ - -struct margin_pos { - /* which blk the read/write starts */ - lbaint_t blk_start; - /* which blk the read/write ends */ - lbaint_t blk_end; - /* start position inside the start blk */ - unsigned long start; - /* end position inside the end blk */ - unsigned long end; - /* how many blks can be read/write one time */ - unsigned long multi; -}; -typedef struct margin_pos margin_pos_t; - static block_dev_desc_t *fs_dev_desc = NULL; static block_dev_desc_t *get_mmc_desc(void) { @@ -42,46 +27,6 @@ static block_dev_desc_t *get_mmc_desc(void) { return get_dev(FSL_AVB_DEV, dev_no); } -/* get margin_pos struct from offset [to the partition start/end] and num_bytes to read/write */ -static int32_t get_margin_pos(lbaint_t part_start, lbaint_t part_end, unsigned long blksz, - margin_pos_t *margin, int64_t offset, size_t num_bytes, bool allow_partial) { - long off; - if (offset < 0) { - margin->blk_start = (offset + 1) / blksz + part_end; - margin->start = (off = offset % blksz) == 0 ? 0 : blksz + off; // offset == -1 means the last byte?, or start need -1 - if (offset + num_bytes - 1 >= 0) { - if (!allow_partial) - return -1; - margin->blk_end = part_end; - margin->end = blksz - 1; - } else { - margin->blk_end = (num_bytes + offset) / blksz + part_end; // which blk the last byte is in - margin->end = (off = (num_bytes + offset - 1) % blksz) == 0 ? - 0 : blksz + off; // last byte - } - } else { - margin->blk_start = offset / blksz + part_start; - margin->start = offset % blksz; - margin->blk_end = (offset + num_bytes - 1) / blksz + part_start ; - margin->end = (offset + num_bytes - 1) % blksz; - if (margin->blk_end > part_end) { - if (!allow_partial) - return -1; - margin->blk_end = part_end; - margin->end = blksz - 1; - } - } - VDEBUG("bs=%ld, be=%ld, s=%ld, e=%ld\n", - margin->blk_start, margin->blk_end, margin->start, margin->end); - - if (margin->blk_start > part_end || margin->blk_start < part_start) - return -1; - long multi = margin->blk_end - margin->blk_start - 1 + - (margin->start == 0) + (margin->end == blksz -1); - margin->multi = multi > 0 ? multi : 0; - VDEBUG("bm=%ld\n", margin->multi); - return 0; -} /* Reads |num_bytes| from offset |offset| from partition with name * |partition| (NUL-terminated UTF-8 string). If |offset| is * negative, its absolute value should be interpreted as the number @@ -135,12 +80,12 @@ static int32_t get_margin_pos(lbaint_t part_start, lbaint_t part_end, unsigned l VDEBUG("blksz: %ld, part_end: %ld, part_start: %ld:\n", blksz, part_end, part_start); - if(get_margin_pos(part_start, part_end, blksz, + if(get_margin_pos((uint64_t)part_start, (uint64_t)part_end, blksz, &margin, offset, num_bytes, true)) return AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION; - bs = margin.blk_start; - be = margin.blk_end; + bs = (lbaint_t)margin.blk_start; + be = (lbaint_t)margin.blk_end; s = margin.start; // alloc a blksz mem @@ -218,12 +163,12 @@ fail: VDEBUG("blksz: %ld, part_end: %ld, part_start: %ld:\n", blksz, part_end, part_start); - if(get_margin_pos(part_start, part_end, blksz, + if(get_margin_pos((uint64_t)part_start, (uint64_t)part_end, blksz, &margin, offset, num_bytes, true)) return AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION; - bs = margin.blk_start; - be = margin.blk_end; + bs = (lbaint_t)margin.blk_start; + be = (lbaint_t)margin.blk_end; s = margin.start; bm = margin.multi; @@ -341,11 +286,11 @@ fail: VDEBUG("blksz: %ld, part_end: %ld, part_start: %ld:\n", blksz, part_end, part_start); - if(get_margin_pos(part_start, part_end, blksz, + if(get_margin_pos((uint64_t)part_start, (uint64_t)part_end, blksz, &margin, offset, num_bytes, false)) return AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION; - bs = margin.blk_start; + bs = (lbaint_t)margin.blk_start; s = margin.start; // alloc a blksz mem diff --git a/lib/avb/fsl/utils.c b/lib/avb/fsl/utils.c new file mode 100644 index 0000000..ec50c70 --- /dev/null +++ b/lib/avb/fsl/utils.c @@ -0,0 +1,51 @@ + +#include <common.h> +#include <stdlib.h> + +#include "debug.h" +#include "utils.h" + +/* get margin_pos struct from offset [to the partition start/end] and num_bytes to read/write */ +int get_margin_pos(uint64_t part_start, uint64_t part_end, unsigned long blksz, + margin_pos_t *margin, int64_t offset, size_t num_bytes, bool allow_partial) { + long off; + assert(margin != NULL); + if (blksz == 0 || part_start > part_end) + return -1; + + if (offset < 0) { + margin->blk_start = (offset + 1) / blksz + part_end; + margin->start = (off = offset % blksz) == 0 ? 0 : blksz + off; // offset == -1 means the last byte?, or start need -1 + if (offset + num_bytes - 1 >= 0) { + if (!allow_partial) + return -1; + margin->blk_end = part_end; + margin->end = blksz - 1; + } else { + margin->blk_end = (num_bytes + offset) / blksz + part_end; // which blk the last byte is in + margin->end = (off = (num_bytes + offset - 1) % blksz) == 0 ? + 0 : blksz + off; // last byte + } + } else { + margin->blk_start = offset / blksz + part_start; + margin->start = offset % blksz; + margin->blk_end = (offset + num_bytes - 1) / blksz + part_start ; + margin->end = (offset + num_bytes - 1) % blksz; + if (margin->blk_end > part_end) { + if (!allow_partial) + return -1; + margin->blk_end = part_end; + margin->end = blksz - 1; + } + } + VDEBUG("bs=%ld, be=%ld, s=%ld, e=%ld\n", + margin->blk_start, margin->blk_end, margin->start, margin->end); + + if (margin->blk_start > part_end || margin->blk_start < part_start) + return -1; + long multi = margin->blk_end - margin->blk_start - 1 + + (margin->start == 0) + (margin->end == blksz -1); + margin->multi = multi > 0 ? multi : 0; + VDEBUG("bm=%ld\n", margin->multi); + return 0; +} diff --git a/lib/avb/fsl/utils.h b/lib/avb/fsl/utils.h new file mode 100644 index 0000000..6fe72f5 --- /dev/null +++ b/lib/avb/fsl/utils.h @@ -0,0 +1,25 @@ +#ifndef __UTILS_H__ +#define __UTILS_H__ + +#include <common.h> + +#define ALIGN_BYTES 64 /*mmc block read/write need 64 bytes aligned */ + +struct margin_pos { + /* which blk the read/write starts */ + uint64_t blk_start; + /* which blk the read/write ends */ + uint64_t blk_end; + /* start position inside the start blk */ + unsigned long start; + /* end position inside the end blk */ + unsigned long end; + /* how many blks can be read/write one time */ + unsigned long multi; +}; +typedef struct margin_pos margin_pos_t; + +int get_margin_pos(uint64_t part_start, uint64_t part_end, unsigned long blksz, + margin_pos_t *margin, int64_t offset, size_t num_bytes, bool allow_partial); + +#endif |