mirror of
https://github.com/smaeul/u-boot.git
synced 2025-10-14 12:56:00 +01:00
mkeficapsule: Miscellaneous fixes in the utility
Miscellaneous fixes in the mkeficapsule utility -- these include a few resource leak issues flagged by Coverity along with some additional code improvements suggested by Heinrich during code review. Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
This commit is contained in:
parent
f7cd8b7b55
commit
d33f31816f
@ -137,8 +137,8 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
bool overlay)
|
bool overlay)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
int srcfd = 0;
|
int srcfd = -1;
|
||||||
int destfd = 0;
|
int destfd = -1;
|
||||||
void *sptr = NULL;
|
void *sptr = NULL;
|
||||||
void *dptr = NULL;
|
void *dptr = NULL;
|
||||||
off_t src_size;
|
off_t src_size;
|
||||||
@ -150,6 +150,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
if (srcfd == -1) {
|
if (srcfd == -1) {
|
||||||
fprintf(stderr, "%s: Can't open %s: %s\n",
|
fprintf(stderr, "%s: Can't open %s: %s\n",
|
||||||
__func__, pkey_file, strerror(errno));
|
__func__, pkey_file, strerror(errno));
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +158,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
fprintf(stderr, "%s: Can't stat %s: %s\n",
|
fprintf(stderr, "%s: Can't stat %s: %s\n",
|
||||||
__func__, pkey_file, strerror(errno));
|
__func__, pkey_file, strerror(errno));
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,9 +166,10 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
|
|
||||||
/* mmap the public key esl file */
|
/* mmap the public key esl file */
|
||||||
sptr = mmap(0, src_size, PROT_READ, MAP_SHARED, srcfd, 0);
|
sptr = mmap(0, src_size, PROT_READ, MAP_SHARED, srcfd, 0);
|
||||||
if ((sptr == MAP_FAILED) || (errno != 0)) {
|
if (sptr == MAP_FAILED) {
|
||||||
fprintf(stderr, "%s: Failed to mmap %s:%s\n",
|
fprintf(stderr, "%s: Failed to mmap %s:%s\n",
|
||||||
__func__, pkey_file, strerror(errno));
|
__func__, pkey_file, strerror(errno));
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,6 +178,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
if (destfd == -1) {
|
if (destfd == -1) {
|
||||||
fprintf(stderr, "%s: Can't open %s: %s\n",
|
fprintf(stderr, "%s: Can't open %s: %s\n",
|
||||||
__func__, dtb_file, strerror(errno));
|
__func__, dtb_file, strerror(errno));
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,21 +193,24 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
if (ftruncate(destfd, dtb.st_size)) {
|
if (ftruncate(destfd, dtb.st_size)) {
|
||||||
fprintf(stderr, "%s: Can't expand %s: %s\n",
|
fprintf(stderr, "%s: Can't expand %s: %s\n",
|
||||||
__func__, dtb_file, strerror(errno));
|
__func__, dtb_file, strerror(errno));
|
||||||
goto err;;
|
ret = -1;
|
||||||
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
/* mmap the dtb file */
|
/* mmap the dtb file */
|
||||||
dptr = mmap(0, dtb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
dptr = mmap(0, dtb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
||||||
destfd, 0);
|
destfd, 0);
|
||||||
if ((dptr == MAP_FAILED) || (errno != 0)) {
|
if (dptr == MAP_FAILED) {
|
||||||
fprintf(stderr, "%s: Failed to mmap %s:%s\n",
|
fprintf(stderr, "%s: Failed to mmap %s:%s\n",
|
||||||
__func__, dtb_file, strerror(errno));
|
__func__, dtb_file, strerror(errno));
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fdt_check_header(dptr)) {
|
if (fdt_check_header(dptr)) {
|
||||||
fprintf(stderr, "%s: Invalid FDT header\n", __func__);
|
fprintf(stderr, "%s: Invalid FDT header\n", __func__);
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,6 +218,7 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
if (ret) {
|
if (ret) {
|
||||||
fprintf(stderr, "%s: Cannot expand FDT: %s\n",
|
fprintf(stderr, "%s: Cannot expand FDT: %s\n",
|
||||||
__func__, fdt_strerror(ret));
|
__func__, fdt_strerror(ret));
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,10 +227,11 @@ static int add_public_key(const char *pkey_file, const char *dtb_file,
|
|||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "%s: Unable to add public key to the FDT\n",
|
fprintf(stderr, "%s: Unable to add public key to the FDT\n",
|
||||||
__func__);
|
__func__);
|
||||||
|
ret = -1;
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
ret = 0;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
if (sptr)
|
if (sptr)
|
||||||
@ -231,13 +240,13 @@ err:
|
|||||||
if (dptr)
|
if (dptr)
|
||||||
munmap(dptr, dtb.st_size);
|
munmap(dptr, dtb.st_size);
|
||||||
|
|
||||||
if (srcfd >= 0)
|
if (srcfd != -1)
|
||||||
close(srcfd);
|
close(srcfd);
|
||||||
|
|
||||||
if (destfd >= 0)
|
if (destfd != -1)
|
||||||
close(destfd);
|
close(destfd);
|
||||||
|
|
||||||
return -1;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
|
static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
|
||||||
@ -424,26 +433,25 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
/* need a fit image file or raw image file */
|
/* need a fit image file or raw image file */
|
||||||
if (!file && !pkey_file && !dtb_file) {
|
if (!file && !pkey_file && !dtb_file) {
|
||||||
printf("%s: %d\n", __func__, __LINE__);
|
|
||||||
print_usage();
|
print_usage();
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pkey_file && dtb_file) {
|
if (pkey_file && dtb_file) {
|
||||||
ret = add_public_key(pkey_file, dtb_file, overlay);
|
ret = add_public_key(pkey_file, dtb_file, overlay);
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
printf("Adding public key to the dtb failed\n");
|
printf("Adding public key to the dtb failed\n");
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (create_fwbin(argv[optind], file, guid, index, instance)
|
if (create_fwbin(argv[optind], file, guid, index, instance)
|
||||||
< 0) {
|
< 0) {
|
||||||
printf("Creating firmware capsule failed\n");
|
printf("Creating firmware capsule failed\n");
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user