diff options
Diffstat (limited to 'futility/cmd_gscvd.c')
-rw-r--r-- | futility/cmd_gscvd.c | 57 |
1 files changed, 22 insertions, 35 deletions
diff --git a/futility/cmd_gscvd.c b/futility/cmd_gscvd.c index e8b2fae1..d6187c9f 100644 --- a/futility/cmd_gscvd.c +++ b/futility/cmd_gscvd.c @@ -143,35 +143,22 @@ struct gscvd_ro_ranges { static int load_ap_firmware(const char *file_name, struct file_buf *file, int mode) { - int fd; - int rv; + if (futil_open_and_map_file(file_name, &file->fd, mode, &file->data, + &file->len)) + return 1; - fd = open(file_name, mode); - if (fd < 0) { - ERROR("Can't open %s: %s\n", file_name, - strerror(errno)); + if (!fmap_find_by_name(file->data, file->len, NULL, "RO_GSCVD", + &file->ro_gscvd)) { + ERROR("Could not find RO_GSCVD in the FMAP\n"); + futil_unmap_and_close_file(file->fd, mode, file->data, + file->len); + file->fd = -1; + file->data = NULL; + file->len = 0; return 1; } - file->fd = fd; - do { - rv = 1; - - if (futil_map_file(fd, mode == O_RDWR ? MAP_RW : MAP_RO, - &file->data, &file->len)) { - file->data = NULL; - break; - } - - if (!fmap_find_by_name(file->data, file->len, NULL, "RO_GSCVD", - &file->ro_gscvd)) { - ERROR("Could not find RO_GSCVD in the FMAP\n"); - break; - } - rv = 0; - } while (false); - - return rv; + return 0; } /** @@ -809,7 +796,7 @@ static int validate_gscvd(int argc, char *argv[]) rv = -1; /* Speculative, will be cleared on success. */ - if (load_ap_firmware(file_name, &ap_firmware_file, O_RDONLY)) + if (load_ap_firmware(file_name, &ap_firmware_file, FILE_RO)) break; /* Copy ranges from gscvd to local structure. */ @@ -857,6 +844,11 @@ static int validate_gscvd(int argc, char *argv[]) rv = 0; } while (false); + if (ap_firmware_file.fd != -1) + futil_unmap_and_close_file(ap_firmware_file.fd, FILE_RO, + ap_firmware_file.data, + ap_firmware_file.len); + return rv; } @@ -1014,7 +1006,7 @@ static int do_gscvd(int argc, char *argv[]) if (validate_privk(kblock, plat_privk)) break; - if (load_ap_firmware(work_file, &ap_firmware_file, O_RDWR)) + if (load_ap_firmware(work_file, &ap_firmware_file, FILE_RW)) break; if (verify_ranges(&ranges, &ap_firmware_file)) @@ -1038,15 +1030,10 @@ static int do_gscvd(int argc, char *argv[]) free(kblock); vb2_private_key_free(plat_privk); - /* Now flush the file. */ - if (ap_firmware_file.data) { - rv |= futil_unmap_file(ap_firmware_file.fd, true, - ap_firmware_file.data, - ap_firmware_file.len); - } - if (ap_firmware_file.fd != -1) - close(ap_firmware_file.fd); + futil_unmap_and_close_file(ap_firmware_file.fd, FILE_RW, + ap_firmware_file.data, + ap_firmware_file.len); return rv; } |