From: Javier Viguera Date: Fri, 15 Feb 2019 09:23:50 +0100 Subject: [PATCH 3/4] fw_env: add support to unlock emmc boot partition Signed-off-by: Javier Viguera Signed-off-by: Arturo Buzarra Signed-off-by: Gonzalo Ruiz --- tools/env/fw_env.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index b124b1e..b87cd83 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -1383,10 +1383,39 @@ err: return rc; } +/* + * Set mmcboot partition read-write protection + */ +static int sysfs_mmcboot_set_protection(const char *device, int value) +{ + int fd; + ssize_t nbytes; + char buf[64]; + snprintf(buf, sizeof(buf), "/sys/block/%s/force_ro", device); + fd = open(buf, O_WRONLY); + if (fd < 0) { + perror("sysfs_mmcboot_set_protection: error opening mmcblk"); + return fd; + } + snprintf(buf, sizeof(buf), "%s", value ? "1" : "0"); + nbytes = write(fd, buf, 2); + close(fd); + + /* Verify bytes written */ + if (nbytes < 2) + { + perror("sysfs_mmcboot_set_protection: error writing mmcblk protection"); + return nbytes >=0 ? -EIO : nbytes; + } + + return 0; +} + static int flash_io_write(int fd_current) { int fd_target = -1, rc, dev_target; const char *dname, *target_temp = NULL; + char *mmcblk = NULL; if (have_redund_env) { /* switch to next partition for writing */ @@ -1414,6 +1443,11 @@ static int flash_io_write(int fd_current) fd_target = fd_current; } + /* Disable mmcboot protection if using EMMC (set read-write) */ + mmcblk = strstr(DEVNAME(dev_target), "mmcblk"); + if (mmcblk) + sysfs_mmcboot_set_protection(mmcblk, 0); + rc = flash_write(fd_current, fd_target, dev_target); if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) { @@ -1465,6 +1499,10 @@ static int flash_io_write(int fd_current) dname, strerror(errno)); } } + + /* Re-enable mmcboot protection (set read-only) */ + if (mmcblk) + sysfs_mmcboot_set_protection(mmcblk, 1); exit: return rc; }