From 50ef0328afa38bbb610613caa687299976047be0 Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Fri, 10 Jun 2022 19:18:13 +0100 Subject: [PATCH] minor cleanups, improve error handling gracefully handle UFS_BSG not being enabled in the kernel, rather than putting the device into a semi-bricked state (oops). --- README.md | 5 ++++ bootctrl_impl.cpp | 5 ++++ gpt-utils.cpp | 2 -- ufs-bsg.cpp | 59 ++++++++++++++--------------------------------- ufs-bsg.h | 7 ++---- 5 files changed, 29 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 26a82dd..e20b9fa 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,8 @@ qbootctl [-c|-m|-s|-u|-b|-n|-x] [SLOT] ## Debugging Set `DEBUG` to 1 in `utils.h` to enable debug logging. + +## Documentation + +A more details explanation and a list of devices where qbootctl has been +validated can be found [on the postmarketOS wiki](https://wiki.postmarketos.org/wiki/Android_AB_Slots): diff --git a/bootctrl_impl.cpp b/bootctrl_impl.cpp index 705efd9..9fdbd6c 100644 --- a/bootctrl_impl.cpp +++ b/bootctrl_impl.cpp @@ -33,6 +33,7 @@ #include "utils.h" #include "gpt-utils.h" +#include "ufs-bsg.h" #include "bootctrl.h" @@ -607,6 +608,10 @@ int set_active_boot_slot(unsigned slot) goto error; } + if (ufs_bsg_dev_open() < 0) { + goto error; + } + //The partition list just contains prefixes(without the _a/_b) of the //partitions that support A/B. In order to get the layout we need the //actual names. To do this we append the slot suffix to every member diff --git a/gpt-utils.cpp b/gpt-utils.cpp index a06eb7f..bb170be 100644 --- a/gpt-utils.cpp +++ b/gpt-utils.cpp @@ -300,8 +300,6 @@ int gpt_utils_set_xbl_boot_partition(enum boot_chain chain) LOGD("%s: setting %s lun as boot lun\n", __func__, boot_dev); if (set_boot_lun(boot_lun_id)) { - fprintf(stderr, "%s: Failed to set xblbak as boot partition\n", - __func__); goto error; } return 0; diff --git a/ufs-bsg.cpp b/ufs-bsg.cpp index 592f803..8b66342 100644 --- a/ufs-bsg.cpp +++ b/ufs-bsg.cpp @@ -45,52 +45,30 @@ #include "utils.h" #include "ufs-bsg.h" -// FIXME: replace this with something that actually works -// static int get_ufs_bsg_dev(void) -// { -// DIR *dir; -// struct dirent *ent; -// int ret = -ENODEV; +/* UFS BSG device node */ +static char ufs_bsg_dev[FNAME_SZ] = "/dev/bsg/ufs-bsg0"; -// if ((dir = opendir ("/dev")) != NULL) { -// /* read all the files and directories within directory */ -// while ((ent = readdir(dir)) != NULL) { -// if (!strcmp(ent->d_name, "bsg") || -// !strcmp(ent->d_name, "ufs-bsg0")) { -// snprintf(ufs_bsg_dev, FNAME_SZ, "/dev/%s", ent->d_name); -// ret = 0; -// break; -// } -// } -// if (ret) -// fprintf(stderr, "could not find the ufs-bsg dev\n"); -// closedir (dir); -// } else { -// /* could not open directory */ -// fprintf(stderr, "could not open /dev (error no: %d)\n", errno); -// ret = -EINVAL; -// } +static int fd_ufs_bsg = 0; -// return ret; -// } - -int ufs_bsg_dev_open(void) +int ufs_bsg_dev_open() { - int ret; - if (!fd_ufs_bsg) { - fd_ufs_bsg = open(ufs_bsg_dev, O_RDWR); - ret = errno; - if (fd_ufs_bsg < 0) { - fprintf(stderr, "Unable to open %s (error no: %d)", - ufs_bsg_dev, errno); - fd_ufs_bsg = 0; - return ret; - } + if (fd_ufs_bsg) + return 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 = 0; + return -1; } + return 0; } -void ufs_bsg_dev_close(void) +void ufs_bsg_dev_close() { if (fd_ufs_bsg) { close(fd_ufs_bsg); @@ -188,9 +166,6 @@ int32_t set_boot_lun(__u8 lun_id) int32_t ret; __u32 boot_lun_id = lun_id; - // ret = get_ufs_bsg_dev(); - // if (ret) - // return ret; LOGD("Using UFS bsg device: %s\n", ufs_bsg_dev); ret = ufs_bsg_dev_open(); diff --git a/ufs-bsg.h b/ufs-bsg.h index d96e814..fdbe31e 100644 --- a/ufs-bsg.h +++ b/ufs-bsg.h @@ -36,11 +36,6 @@ #define DWORD(b3, b2, b1, b0) htobe32((b3 << 24) | (b2 << 16) | (b1 << 8) | b0) -/* UFS BSG device node */ -char ufs_bsg_dev[FNAME_SZ] = "/dev/bsg/ufs-bsg0"; - -int fd_ufs_bsg; - /* UPIU Transaction Codes */ enum { UTP_UPIU_NOP_OUT = 0x00, @@ -91,4 +86,6 @@ enum query_attr_idn { QUERY_ATTR_IDN_ACTIVE_ICC_LVL = 0x03, }; +int ufs_bsg_dev_open(); + #endif /* __RECOVERY_UFS_BSG_H__ */