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

This commit introduced a lot of changes and seems to have caused a few
issues, including breaking crc32 generation. Revert it for now with the
intention to reimplement some of the improvements.

This reverts commit 9d7600df51.
This commit is contained in:
Caleb Connolly
2023-06-23 13:30:08 +01:00
parent 20572c1417
commit 75e30f8d09
5 changed files with 344 additions and 328 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -96,32 +96,36 @@ enum boot_chain { NORMAL_BOOT = 0, BACKUP_BOOT };
struct gpt_disk {
//GPT primary header
std::vector<uint8_t> hdr;
uint8_t *hdr;
//primary header crc
uint32_t hdr_crc = 0;
uint32_t hdr_crc;
//GPT backup header
std::vector<uint8_t> hdr_bak;
uint8_t *hdr_bak;
//backup header crc
uint32_t hdr_bak_crc = 0;
uint32_t hdr_bak_crc;
//Partition entries array
std::vector<uint8_t> pentry_arr;
uint8_t *pentry_arr;
//Partition entries array for backup table
std::vector<uint8_t> pentry_arr_bak;
uint8_t *pentry_arr_bak;
//Size of the pentry array
uint32_t pentry_arr_size = 0;
uint32_t pentry_arr_size;
//Size of each element in the pentry array
uint32_t pentry_size = 0;
uint32_t pentry_size;
//CRC of the partition entry array
uint32_t pentry_arr_crc = 0;
uint32_t pentry_arr_crc;
//CRC of the backup partition entry array
uint32_t pentry_arr_bak_crc = 0;
uint32_t pentry_arr_bak_crc;
//Path to block dev representing the disk
char devpath[PATH_MAX] = { 0 };
char devpath[PATH_MAX];
//Block size of disk
uint32_t block_size = 0;
uint32_t is_initialized = 0;
uint32_t block_size;
uint32_t is_initialized;
};
//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,23 +41,17 @@ 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))
unexpectedSlot(arg);
if (!isslot(arg)) {
goto fail;
}
if (isslotnum(arg)) {
slot = (int)strtol(arg, &end, 10);
if (end == arg)
unexpectedSlot(arg);
goto fail;
} else {
switch (arg[0]) {
case 'a':
@@ -69,11 +63,17 @@ unsigned parseSlot(const char* arg)
slot = 1;
break;
default:
unexpectedSlot(arg);
goto fail;
}
}
return (unsigned)slot;
fail:
fprintf(stderr,
"Expected slot not '%s'\n",
arg);
exit(1);
}
int usage()

View File

@@ -48,43 +48,34 @@
/* UFS BSG device node */
static char ufs_bsg_dev[FNAME_SZ] = "/dev/bsg/ufs-bsg0";
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;
static int fd_ufs_bsg = 0;
int ufs_bsg_dev_open()
{
if (fd_ufs_bsg.fd)
if (fd_ufs_bsg)
return 0;
fd_ufs_bsg.fd = open(ufs_bsg_dev, O_RDWR);
if (fd_ufs_bsg.fd < 0) {
fd_ufs_bsg = open(ufs_bsg_dev, O_RDWR);
if (fd_ufs_bsg < 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.fd = 0;
fd_ufs_bsg = 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)
@@ -180,18 +171,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.fd, 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, 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;
}
fd_ufs_bsg.close();
out:
ufs_bsg_dev_close();
return ret;
}