diff options
Diffstat (limited to 'common/imd.c')
-rw-r--r-- | common/imd.c | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/common/imd.c b/common/imd.c index 9be07fef74..1100e6878a 100644 --- a/common/imd.c +++ b/common/imd.c @@ -1,19 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * (C) Copyright 2014 Sascha Hauer, Pengutronix - * - * See file CREDITS for list of people who contributed to this - * project. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * */ #ifdef __BAREBOX__ #include <common.h> @@ -30,6 +17,16 @@ int imd_command_setenv(const char *variable_name, const char *value) return -ENOSYS; } #endif +static inline void read_file_2_free(void *buf) +{ + free(buf); +} + +static int imd_read_file(const char *filename, size_t *size, void **outbuf, + bool allow_mmap) +{ + return read_file_2(filename, size, outbuf, 0x100000); +} #endif /* @@ -171,6 +168,9 @@ static struct imd_type_names imd_types[] = { }, { .type = IMD_TYPE_CRC32, .name = "crc32", + }, { + .type = IMD_TYPE_BUILDSYSTEM, + .name = "buildsystem version", }, }; @@ -315,6 +315,7 @@ static int imd_calculate_crc32(void *input, const struct imd_header *imd_start, const struct imd_header *imd; int length; int end_ofs = (char *)imd_start - (char *)input + sizeof(char) * 8; + *imd_crc = NULL; /* search the checksum imd token */ imd_for_each(imd_start, imd) { @@ -322,7 +323,7 @@ static int imd_calculate_crc32(void *input, const struct imd_header *imd_start, length = ALIGN(length, 4); length += sizeof(struct imd_header); - if (imd_read_type(imd) == IMD_TYPE_CRC32) { + if (imd_is_crc32(imd_read_type(imd))) { *imd_crc = (struct imd_header *)imd; debug("Found crc token at %d\n", end_ofs); break; @@ -349,7 +350,7 @@ static int imd_calculate_crc32(void *input, const struct imd_header *imd_start, input += end_ofs; *crc = crc32(*crc, input, size - end_ofs); - debug("Calculated checksum from %d to %d: 0x%08x\n", end_ofs, + debug("Calculated checksum from %d to %zu: 0x%08x\n", end_ofs, end_ofs + (size - end_ofs), *crc); return 0; @@ -370,6 +371,7 @@ static int imd_write_crc32(void *buf, const struct imd_header *imd_start, return -ENODATA; } else { uint32_t *p = (uint32_t *)(imd_crc + 1); + int ret; if (*p != crc) { uint32_t *flags = imd_crc32_flags(imd_crc); @@ -377,7 +379,11 @@ static int imd_write_crc32(void *buf, const struct imd_header *imd_start, debug("Update crc token from 0x%08x to 0x%08x (flags 0x%08x)\n", *p, crc, *flags); *p = crc; - write_file(filename, buf, size); + ret = write_file(filename, buf, size); + if (ret < 0) { + eprintf("CRC: write crc token to %s failed: %d\n", filename, ret); + return ret; + } } } @@ -410,7 +416,7 @@ int imd_verify_crc32(void *buf, size_t size) *p, crc); return -EILSEQ; } else if (*p != crc && !imd_crc32_is_valid(*flags)) { - printf("CRC: is invalid, but the checksum tag is not enabled\n"); + debug("CRC: is invalid, but the checksum tag is not enabled\n"); return -EINVAL; } else { printf("CRC: valid\n"); @@ -434,6 +440,7 @@ int imd_command(int argc, char *argv[]) char *str; uint32_t checksum = 0; uint32_t verify = 0; + bool allow_mmap = true; imd_command_verbose = 0; @@ -457,6 +464,7 @@ int imd_command(int argc, char *argv[]) break; case 'c': checksum = 1; + allow_mmap = false; break; case 'V': verify = 1; @@ -473,7 +481,7 @@ int imd_command(int argc, char *argv[]) filename = argv[optind]; - ret = read_file_2(filename, &size, &buf, 0x100000); + ret = imd_read_file(filename, &size, &buf, allow_mmap); if (ret && ret != -EFBIG) return -errno; @@ -483,10 +491,15 @@ int imd_command(int argc, char *argv[]) goto out; } - if (checksum) - imd_write_crc32(buf, imd_start, filename, size); - if (verify) - imd_verify_crc32(buf, size); + if (checksum) { + ret = imd_write_crc32(buf, imd_start, filename, size); + goto out; + } + + if (verify) { + ret = imd_verify_crc32(buf, size); + goto out; + } if (type == IMD_TYPE_INVALID) { imd_for_each(imd_start, imd) { @@ -541,6 +554,6 @@ int imd_command(int argc, char *argv[]) ret = 0; out: - free(buf); + read_file_2_free(buf); return ret; } |