Use scope-based memory management for malloc, free, open, close, etc.

If we are using C++, we might as well use scope-based memory management
for these things, cleaner and less error prone. Also removed
unnecessary gotos.
This commit is contained in:
Eric Curtin
2023-05-08 23:49:26 +01:00
committed by Caleb Connolly
parent 843aa92266
commit 9d7600df51
5 changed files with 331 additions and 347 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -96,36 +96,32 @@ enum boot_chain { NORMAL_BOOT = 0, BACKUP_BOOT };
struct gpt_disk {
//GPT primary header
uint8_t *hdr;
std::vector<uint8_t> hdr;
//primary header crc
uint32_t hdr_crc;
uint32_t hdr_crc = 0;
//GPT backup header
uint8_t *hdr_bak;
std::vector<uint8_t> hdr_bak;
//backup header crc
uint32_t hdr_bak_crc;
uint32_t hdr_bak_crc = 0;
//Partition entries array
uint8_t *pentry_arr;
std::vector<uint8_t> pentry_arr;
//Partition entries array for backup table
uint8_t *pentry_arr_bak;
std::vector<uint8_t> pentry_arr_bak;
//Size of the pentry array
uint32_t pentry_arr_size;
uint32_t pentry_arr_size = 0;
//Size of each element in the pentry array
uint32_t pentry_size;
uint32_t pentry_size = 0;
//CRC of the partition entry array
uint32_t pentry_arr_crc;
uint32_t pentry_arr_crc = 0;
//CRC of the backup partition entry array
uint32_t pentry_arr_bak_crc;
uint32_t pentry_arr_bak_crc = 0;
//Path to block dev representing the disk
char devpath[PATH_MAX];
char devpath[PATH_MAX] = { 0 };
//Block size of disk
uint32_t block_size;
uint32_t is_initialized;
uint32_t block_size = 0;
uint32_t is_initialized = 0;
};
//GPT disk methods
struct gpt_disk *gpt_disk_alloc();
//Free previously allocated gpt_disk struct
void gpt_disk_free(struct gpt_disk *disk);
//Get the details of the disk holding the partition whose name
//is passed in via dev
int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *disk);

View File

@@ -41,17 +41,23 @@ bool isslotnum(const char* str)
return strspn(str, "01") == strlen(str);
}
static void unexpectedSlot(const char *arg)
{
fprintf(stderr, "Expected slot not '%s'\n", arg);
exit(1);
}
unsigned parseSlot(const char* arg)
{
char *end;
int slot;
if (!isslot(arg)) {
goto fail;
}
if (!isslot(arg))
unexpectedSlot(arg);
if (isslotnum(arg)) {
slot = (int)strtol(arg, &end, 10);
if (end == arg)
goto fail;
unexpectedSlot(arg);
} else {
switch (arg[0]) {
case 'a':
@@ -63,17 +69,11 @@ unsigned parseSlot(const char* arg)
slot = 1;
break;
default:
goto fail;
unexpectedSlot(arg);
}
}
return (unsigned)slot;
fail:
fprintf(stderr,
"Expected slot not '%s'\n",
arg);
exit(1);
}
int usage()

View File

@@ -48,34 +48,43 @@
/* UFS BSG device node */
static char ufs_bsg_dev[FNAME_SZ] = "/dev/bsg/ufs-bsg0";
static int fd_ufs_bsg = 0;
struct fd_ufs_bsg {
int fd = 0;
~fd_ufs_bsg()
{
close();
}
void close()
{
if (fd > 0) {
::close(fd);
fd = 0;
}
}
};
static fd_ufs_bsg fd_ufs_bsg;
int ufs_bsg_dev_open()
{
if (fd_ufs_bsg)
if (fd_ufs_bsg.fd)
return 0;
fd_ufs_bsg = open(ufs_bsg_dev, O_RDWR);
if (fd_ufs_bsg < 0) {
fd_ufs_bsg.fd = open(ufs_bsg_dev, O_RDWR);
if (fd_ufs_bsg.fd < 0) {
fprintf(stderr, "Unable to open '%s': %s\n", ufs_bsg_dev,
strerror(errno));
fprintf(stderr,
"Is CONFIG_SCSI_UFS_BSG is enabled in your kernel?\n");
fd_ufs_bsg = 0;
fd_ufs_bsg.fd = 0;
return -1;
}
return 0;
}
void ufs_bsg_dev_close()
{
if (fd_ufs_bsg) {
close(fd_ufs_bsg);
fd_ufs_bsg = 0;
}
}
static int ufs_bsg_ioctl(int fd, struct ufs_bsg_request *req,
struct ufs_bsg_reply *rsp, __u8 *buf, __u32 buf_len,
enum bsg_ioctl_dir dir)
@@ -171,18 +180,18 @@ int32_t set_boot_lun(__u8 lun_id)
ret = ufs_bsg_dev_open();
if (ret)
return ret;
LOGD("Opened ufs bsg dev: %s\n", ufs_bsg_dev);
ret = ufs_query_attr(fd_ufs_bsg, boot_lun_id, QUERY_REQ_FUNC_STD_WRITE,
QUERY_REQ_OP_WRITE_ATTR, QUERY_ATTR_IDN_BOOT_LU_EN,
0, 0);
ret = ufs_query_attr(fd_ufs_bsg.fd, boot_lun_id,
QUERY_REQ_FUNC_STD_WRITE, QUERY_REQ_OP_WRITE_ATTR,
QUERY_ATTR_IDN_BOOT_LU_EN, 0, 0);
if (ret) {
fprintf(stderr,
"Error requesting ufs attr idn %d via query ioctl (return value: %d, error no: %d)",
QUERY_ATTR_IDN_BOOT_LU_EN, ret, errno);
goto out;
}
out:
ufs_bsg_dev_close();
fd_ufs_bsg.close();
return ret;
}