Refactor generic fastboot_set_reboot_flag implementation
cover Refactor generic fastboot_set_reboot_flag implementation
Current generic implementation of fastboot_set_reboot_flag is somewhat messy and requires some additional configuration option to be enabled besides CMD_BCB, so it reverts that implementtion in order to bring a new cleaner one.
New function called bcb_set_reboot_reason should be exposed by BCB commands, so that all of the details could be put there instead of dealing with it in fastboot implementation directly. After that, fastboot_set_reboot_flag could simply pass correct reboot reason string to the BCB implementation. If CMD_BCB is disabled then the whole operation would return error code, which is no different behaviour than the current implementation.
Eugeniu Rosca (5):
cmd: bcb: Extract '__bcb_load' from 'do_bcb_load' for internal needs
cmd: bcb: Extract '__bcb_set' from 'do_bcb_set' for internal needs
cmd: bcb: Extract '__bcb_store' from 'do_bcb_store' for internal needs
cmd: bcb: Expose 'bcb_write_reboot_reason' to external callers
cmd: bcb: Add support for processing const string literals in
bcb_set()
Roman Kovalivskyi (2):
Revert "fastboot: Add default fastboot_set_reboot_flag implementation"
fastboot: Implement generic fastboot_set_reboot_flag
cmd/bcb.c | 88 +++++++++++++++++++++++++++------- drivers/fastboot/Kconfig | 12 ----- drivers/fastboot/Makefile | 1 - drivers/fastboot/fb_bcb_impl.c | 43 ----------------- drivers/fastboot/fb_common.c | 12 ++++- include/bcb.h | 20 ++++++++ include/fastboot.h | 9 ---- 7 files changed, 101 insertions(+), 84 deletions(-) delete mode 100644 drivers/fastboot/fb_bcb_impl.c create mode 100644 include/bcb.h
Signature · 1 line
2.17.1
PATCH 1/7 cmd: bcb: Extract '__bcb_load' from 'do_bcb_load' for internal needs
From: Eugeniu Rosca <[email protected]>
Enriching the functionality of U-Boot 'bcb' may assume using the existing sub-commands as building blocks for the next ones.
A clean way to achive the above is to expose a number of static routines, each mapped to an existing user command (e.g. load/set/store), with a user/caller-friendly prototype (i.e. do not force the caller to wrap an integer into a string).
This first patch makes '__bcb_load' available for internal needs.
No functional change, except for a tiny update in error handling.
--- cmd/bcb.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
cmd/bcb.c +23 -8
@@ -110,8 +110,7 @@ static int bcb_field_get(char *name, char **fieldp, int *sizep)return 0 ; } -static int do_bcb_load (struct cmd_tbl *cmdtp,int flag,int argc, -char *const argv[]) +static int __bcb_load (int devnum,const char *partp) {struct blk_desc *desc;struct disk_partition info;
@@ -119,17 +118,19 @@ static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,char *endp;int part, ret; - ret =blk_get_device_by_str ("mmc" , argv[1 ], &desc); -if (ret <0 ) + desc =blk_get_devnum_by_type (IF_TYPE_MMC , devnum); +if (!desc) { + ret = -ENODEV ; goto err_read_fail; + } - part =simple_strtoul (argv[2 ], &endp,0 ); + part =simple_strtoul (partp, &endp,0 );if (*endp =='\0' ) { ret =part_get_info (desc, part, &info);if (ret) goto err_read_fail; }else { - part =part_get_info_by_name (desc, argv[2 ], &info); + part =part_get_info_by_name (desc, partp, &info);if (part <0 ) { ret = part; goto err_read_fail;
@@ -151,10 +152,10 @@ static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,return CMD_RET_SUCCESS ; err_read_fail: -printf ("Error: mmc %s:%s read failed (%d)\n" , argv[1 ], argv[2 ], ret); +printf ("Error: mmc %d:%s read failed (%d)\n" , devnum, partp, ret); goto err; err_too_small: -printf ("Error: mmc %s:%s too small!" , argv[1 ], argv[2 ]); +printf ("Error: mmc %d:%s too small!" , devnum, partp); goto err; err: bcb_dev =-1 ;
@@ -163,6 +164,20 @@ err:return CMD_RET_FAILURE ; } +static int do_bcb_load (struct cmd_tbl *cmdtp,int flag,int argc, +char *const argv[]) +{ +char *endp; +int devnum =simple_strtoul (argv[1 ], &endp,0 ); + +if (*endp !='\0' ) { +printf ("Error: Device id '%s' not a number\n" , argv[1 ]); +return CMD_RET_FAILURE ; + } + +return __bcb_load (devnum, argv[2 ]); +} +static int do_bcb_set (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]) {
Signature · 1 line
2.17.1
PATCH 1/2 Revert "fastboot: Add default fastboot_set_reboot_flag implementation"
In reply to Roman KovalivskyiThis reverts commit 0ebf9842e56c5b8cb7cb1f990bb452cc14af6225.
Current generic implementation of fastboot_set_reboot_flag is somewhat messy and requires some additional configuration option to be enabled besides CMD_BCB, so it reverts that implementtion in order to bring a new cleaner one.
Next commit introduces new generic implementation of fastboot_set_reboot_flag.
--- drivers/fastboot/Kconfig | 12 ---------- drivers/fastboot/Makefile | 1 - drivers/fastboot/fb_bcb_impl.c | 43 ---------------------------------- include/fastboot.h | 9 ------- 4 files changed, 65 deletions(-) delete mode 100644 drivers/fastboot/fb_bcb_impl.c
drivers/fastboot/Kconfig +0 -12
@@ -165,18 +165,6 @@ config FASTBOOT_CMD_OEM_FORMAT relies on the env variable partitions to contain the list of partitions as required by the gpt command. -config FASTBOOT_USE_BCB_SET_REBOOT_FLAG - bool "Use BCB by fastboot to set boot reason" - depends on CMD_BCB && !ARCH_MESON && !ARCH_ROCKCHIP && !TARGET_KC1 && \ - !TARGET_SNIPER && !TARGET_AM57XX_EVM && !TARGET_DRA7XX_EVM - default y - help - Fastboot could implement setting of reboot reason in a generic fashion - via BCB commands. BCB commands are able to write reboot reason into - command field of boot control block. In general case it is sufficient - implementation if your platform supports BCB commands and doesn't - require any specific reboot reason handling. - endif # FASTBOOT endmenu
drivers/fastboot/Makefile +0 -1
@@ -5,4 +5,3 @@ obj-y += fb_getvar.o obj-y += fb_command.o obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fb_mmc.o obj-$(CONFIG_FASTBOOT_FLASH_NAND) += fb_nand.o -obj-$(CONFIG_FASTBOOT_USE_BCB_SET_REBOOT_FLAG) += fb_bcb_impl.o
drivers/fastboot/fb_bcb_impl.c deleted +0 -43
43 lines not on this page · raw message
include/fastboot.h +0 -9
@@ -52,15 +52,6 @@ enum fastboot_reboot_reason { FASTBOOT_REBOOT_REASONS_COUNT }; -/** -* BCB boot commands -*/ -static const char *const fastboot_boot_cmds[] = { - [FASTBOOT_REBOOT_REASON_BOOTLOADER ] ="bootonce-bootloader" , - [FASTBOOT_REBOOT_REASON_FASTBOOTD ] ="boot-fastboot" , - [FASTBOOT_REBOOT_REASON_RECOVERY ] ="boot-recovery" -}; -/** * fastboot_response() - Writes a response of the form "$tag$reason". *
Signature · 1 line
2.17.1
PATCH 2/7 cmd: bcb: Extract '__bcb_set' from 'do_bcb_set' for internal needs
In reply to Roman KovalivskyiFrom: Eugeniu Rosca <[email protected]>
Enriching the functionality of U-Boot 'bcb' may assume using the existing sub-commands as building blocks for the next ones.
A clean way to achive the above is to expose a number of static routines, each mapped to an existing user command (e.g. load/set/store), with a user/caller-friendly prototype (i.e. do not force the caller to wrap an integer into a string).
This second patch makes '__bcb_set' available for internal needs.
No functional change intended.
--- cmd/bcb.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
cmd/bcb.c +11 -6
@@ -178,22 +178,21 @@ static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,return __bcb_load (devnum, argv[2 ]); } -static int do_bcb_set (struct cmd_tbl *cmdtp,int flag,int argc, -char *const argv[]) +static int __bcb_set (char *fieldp,char *valp) {int size, len;char *field, *str, *found; -if (bcb_field_get (argv[1 ], &field, &size)) +if (bcb_field_get (fieldp, &field, &size))return CMD_RET_FAILURE ; - len =strlen (argv[2 ]); + len =strlen (valp);if (len >= size) {printf ("Error: sizeof('%s') = %d >= %d = sizeof(bcb.%s)\n" , - argv[2 ], len, size, argv[1 ]); + valp, len, size, fieldp);return CMD_RET_FAILURE ; } - str = argv[2 ]; + str = valp; field[0 ] ='\0' ;while ((found =strsep (&str,":" ))) {
@@ -205,6 +204,12 @@ static int do_bcb_set(struct cmd_tbl *cmdtp, int flag, int argc,return CMD_RET_SUCCESS ; } +static int do_bcb_set (struct cmd_tbl *cmdtp,int flag,int argc, +char *const argv[]) +{ +return __bcb_set (argv[1 ], argv[2 ]); +} +static int do_bcb_clear (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]) {
Signature · 1 line
2.17.1
PATCH 2/2 fastboot: Implement generic fastboot_set_reboot_flag
In reply to Roman KovalivskyiIt is possible to implement fastboot_set_reboot_flag in a generic way if BCB commands are turned on for a target. Using bcb_set_reboot_reason allows to do this by simply passing string with correct reboot reason that should be handled during next boot process.
If BCB are turned off, then bcb_set_reboot_reason would simply return error, so it won't introduce any new behaviour for such targets.
--- drivers/fastboot/fb_common.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/fastboot/fb_common.c +11 -1
@@ -10,6 +10,7 @@* Rob Herring <[email protected]> */ +#include <bcb.h> #include <common.h> #include <command.h> #include <env.h>
@@ -90,7 +91,16 @@ void fastboot_okay(const char *reason, char *response)*/ int __weak fastboot_set_reboot_flag (enum fastboot_reboot_reason reason) { -return -ENOSYS ; +static const char *const boot_cmds[] = { + [FASTBOOT_REBOOT_REASON_BOOTLOADER ] ="bootonce-bootloader" , + [FASTBOOT_REBOOT_REASON_FASTBOOTD ] ="boot-fastboot" , + [FASTBOOT_REBOOT_REASON_RECOVERY ] ="boot-recovery" + }; + +if (reason >=FASTBOOT_REBOOT_REASONS_COUNT ) +return -EINVAL ; + +return bcb_write_reboot_reason (CONFIG_FASTBOOT_FLASH_MMC_DEV ,"misc" , boot_cmds[reason]); }/**
Signature · 1 line
2.17.1
PATCH 3/7 cmd: bcb: Extract '__bcb_store' from 'do_bcb_store' for internal needs
In reply to Roman KovalivskyiFrom: Eugeniu Rosca <[email protected]>
Enriching the functionality of U-Boot 'bcb' may assume using the existing sub-commands as building blocks for the next ones.
A clean way to achive the above is to expose a number of static routines, each mapped to an existing user command (e.g. load/set/store), with a user/caller-friendly prototype (i.e. do not force the caller to wrap an integer into a string).
This third patch makes '__bcb_store' available for internal needs.
No functional change intended.
--- cmd/bcb.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
cmd/bcb.c +7 -2
@@ -270,8 +270,7 @@ static int do_bcb_dump(struct cmd_tbl *cmdtp, int flag, int argc,return CMD_RET_SUCCESS ; } -static int do_bcb_store (struct cmd_tbl *cmdtp,int flag,int argc, -char *const argv[]) +static int __bcb_store (void ) {struct blk_desc *desc;struct disk_partition info;
@@ -302,6 +301,12 @@ err:return CMD_RET_FAILURE ; } +static int do_bcb_store (struct cmd_tbl *cmdtp,int flag,int argc, +char *const argv[]) +{ +return __bcb_store (); +} +static struct cmd_tbl cmd_bcb_sub[] = {U_BOOT_CMD_MKENT (load,CONFIG_SYS_MAXARGS ,1 , do_bcb_load,"" ,"" ),U_BOOT_CMD_MKENT (set,CONFIG_SYS_MAXARGS ,1 , do_bcb_set,"" ,"" ),
Signature · 1 line
2.17.1
PATCH 4/7 cmd: bcb: Expose 'bcb_write_reboot_reason' to external callers
In reply to Roman KovalivskyiFrom: Eugeniu Rosca <[email protected]>
Fastboot is evolving and beginning with commit [1], the upstream implementation expects bootloaders to offer support for:
- reboot-recovery
- reboot-fastboot
The most natural way to achieve the above is through a set of pre-defined "reboot reason" strings, written into / read from the BCB "command" field, e.g.:
- bootonce-bootloader [2]
- boot-fastboot [3]
- boot-recovery [4]
Expose the first 'bcb' API meant to be called by e.g. fastboot stack, to allow updating the BCB reboot reason via the BCB 'command' field.
[1] https://android.googlesource.com/platform/system/core/+/dea91b4b5354af2
("Add fastbootd.")[2] https://android.googlesource.com/platform/bootable/recovery/+/cba7fa88d8b9
("Add 'reboot bootloader' to bootloader_message.")
[3] https://android.googlesource.com/platform/bootable/recovery/+/eee4e260f9f6
("recovery: Add "boot-fastboot" command to BCB.")
[4] https://android.googlesource.com/platform/system/core/+/5e98b633a748695f
("init: Write the reason in BCB on "reboot recovery"")
--- cmd/bcb.c | 20 ++++++++++++++++++++ include/bcb.h | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 include/bcb.h
cmd/bcb.c +20 -0
@@ -6,6 +6,7 @@*/ #include <android_bootloader_message.h> +#include <bcb.h> #include <command.h> #include <common.h> #include <log.h>
@@ -307,6 +308,25 @@ static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,return __bcb_store (); } +int bcb_write_reboot_reason (int devnum,char *partp,char *reasonp) +{ +int ret; + + ret =__bcb_load (devnum, partp); +if (ret !=CMD_RET_SUCCESS ) +return ret; + + ret =__bcb_set ("command" , reasonp); +if (ret !=CMD_RET_SUCCESS ) +return ret; + + ret =__bcb_store (); +if (ret !=CMD_RET_SUCCESS ) +return ret; + +return 0 ; +} +static struct cmd_tbl cmd_bcb_sub[] = {U_BOOT_CMD_MKENT (load,CONFIG_SYS_MAXARGS ,1 , do_bcb_load,"" ,"" ),U_BOOT_CMD_MKENT (set,CONFIG_SYS_MAXARGS ,1 , do_bcb_set,"" ,"" ),
include/bcb.h new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2020 Eugeniu Rosca <[email protected]> +* +* Android Bootloader Control Block Header +*/ + +#ifndef __BCB_H__ +#define __BCB_H__ + +#if CONFIG_IS_ENABLED (CMD_BCB ) +int bcb_write_reboot_reason (int devnum,char *partp,char *reasonp); +#else +static inline int bcb_write_reboot_reason (int devnum,char *partp,char *reasonp) +{ +return -EOPNOTSUPP ; +} +#endif + +#endif /* __BCB_H__ */
Signature · 1 line
2.17.1
PATCH 5/7 cmd: bcb: Add support for processing const string literals in bcb_set()
In reply to Roman KovalivskyiFrom: Eugeniu Rosca <[email protected]>
On request/suggestion from Simon Glass back in May 22 2019 [1], the 'strsep' mechanism implemented in bcb_set() was set to work directly with user-provided argv strings, to avoid duplicating memory and for the sake of simpler implementation.
However, since we recently exposed bcb_write_reboot_reason() API to be called by U-Boot fastboot, the idea is to be able to pass const string literals to this new BCB API, carrying the reboot reason.
Since 'strsep' (just like its older/superseded sibling 'strtok') modifies the input string passed as parameter, BCB command in its current state would attempt to perform in-place modifications in a readonly string, which might lead to unexpected results.
Fix the above with the cost of one dynamic memory allocation ('strdup'). This will also ensure no compiler warnings when passing string literals to bcb_write_reboot_reason().
--- cmd/bcb.c | 17 ++++++++++++----- include/bcb.h | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-)
cmd/bcb.c +12 -5
@@ -11,6 +11,7 @@#include <common.h> #include <log.h> #include <part.h> +#include <malloc.h> enum bcb_cmd {BCB_CMD_LOAD ,
@@ -179,10 +180,10 @@ static int do_bcb_load(struct cmd_tbl *cmdtp, int flag, int argc,return __bcb_load (devnum, argv[2 ]); } -static int __bcb_set (char *fieldp,char *valp) +static int __bcb_set (char *fieldp,const char *valp) {int size, len; -char *field, *str, *found; +char *field, *str, *found, *tmp;if (bcb_field_get (fieldp, &field, &size))return CMD_RET_FAILURE ;
@@ -193,14 +194,20 @@ static int __bcb_set(char *fieldp, char *valp) valp, len, size, fieldp);return CMD_RET_FAILURE ; } - str = valp; + str =strdup (valp); +if (!str) { +printf ("Error: Out of memory while strdup\n" ); +return CMD_RET_FAILURE ; + } + tmp = str; field[0 ] ='\0' ; -while ((found =strsep (&str,":" ))) { +while ((found =strsep (&tmp,":" ))) {if (field[0 ] !='\0' )strcat (field,"\n" );strcat (field, found); } +free (str);return CMD_RET_SUCCESS ; }
@@ -308,7 +315,7 @@ static int do_bcb_store(struct cmd_tbl *cmdtp, int flag, int argc,return __bcb_store (); } -int bcb_write_reboot_reason (int devnum,char *partp,char *reasonp) +int bcb_write_reboot_reason (int devnum,char *partp,const char *reasonp) {int ret;
include/bcb.h +2 -2
@@ -9,9 +9,9 @@#define __BCB_H__#if CONFIG_IS_ENABLED (CMD_BCB ) -int bcb_write_reboot_reason (int devnum,char *partp,char *reasonp); +int bcb_write_reboot_reason (int devnum,char *partp,const char *reasonp);#else -static inline int bcb_write_reboot_reason (int devnum,char *partp,char *reasonp) +static inline int bcb_write_reboot_reason (int devnum,char *partp,const char *reasonp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.17.1
PATCH 6/7 Revert "fastboot: Add default fastboot_set_reboot_flag implementation"
In reply to Roman KovalivskyiThis reverts commit 0ebf9842e56c5b8cb7cb1f990bb452cc14af6225.
Current generic implementation of fastboot_set_reboot_flag is somewhat messy and requires some additional configuration option to be enabled besides CMD_BCB, so it reverts that implementtion in order to bring a new cleaner one.
Next commit introduces new generic implementation of fastboot_set_reboot_flag.
--- drivers/fastboot/Kconfig | 12 ---------- drivers/fastboot/Makefile | 1 - drivers/fastboot/fb_bcb_impl.c | 43 ---------------------------------- include/fastboot.h | 9 ------- 4 files changed, 65 deletions(-) delete mode 100644 drivers/fastboot/fb_bcb_impl.c
drivers/fastboot/Kconfig +0 -12
@@ -165,18 +165,6 @@ config FASTBOOT_CMD_OEM_FORMAT relies on the env variable partitions to contain the list of partitions as required by the gpt command. -config FASTBOOT_USE_BCB_SET_REBOOT_FLAG - bool "Use BCB by fastboot to set boot reason" - depends on CMD_BCB && !ARCH_MESON && !ARCH_ROCKCHIP && !TARGET_KC1 && \ - !TARGET_SNIPER && !TARGET_AM57XX_EVM && !TARGET_DRA7XX_EVM - default y - help - Fastboot could implement setting of reboot reason in a generic fashion - via BCB commands. BCB commands are able to write reboot reason into - command field of boot control block. In general case it is sufficient - implementation if your platform supports BCB commands and doesn't - require any specific reboot reason handling. - endif # FASTBOOT endmenu
drivers/fastboot/Makefile +0 -1
@@ -5,4 +5,3 @@ obj-y += fb_getvar.o obj-y += fb_command.o obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fb_mmc.o obj-$(CONFIG_FASTBOOT_FLASH_NAND) += fb_nand.o -obj-$(CONFIG_FASTBOOT_USE_BCB_SET_REBOOT_FLAG) += fb_bcb_impl.o
drivers/fastboot/fb_bcb_impl.c deleted +0 -43
43 lines not on this page · raw message
include/fastboot.h +0 -9
@@ -52,15 +52,6 @@ enum fastboot_reboot_reason { FASTBOOT_REBOOT_REASONS_COUNT }; -/** -* BCB boot commands -*/ -static const char *const fastboot_boot_cmds[] = { - [FASTBOOT_REBOOT_REASON_BOOTLOADER ] ="bootonce-bootloader" , - [FASTBOOT_REBOOT_REASON_FASTBOOTD ] ="boot-fastboot" , - [FASTBOOT_REBOOT_REASON_RECOVERY ] ="boot-recovery" -}; -/** * fastboot_response() - Writes a response of the form "$tag$reason". *
Signature · 1 line
2.17.1
PATCH 7/7 fastboot: Implement generic fastboot_set_reboot_flag
In reply to Roman KovalivskyiIt is possible to implement fastboot_set_reboot_flag in a generic way if BCB commands are turned on for a target. Using bcb_set_reboot_reason allows to do this by simply passing string with correct reboot reason that should be handled during next boot process.
If BCB are turned off, then bcb_set_reboot_reason would simply return error, so it won't introduce any new behaviour for such targets.
--- drivers/fastboot/fb_common.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/fastboot/fb_common.c +11 -1
@@ -10,6 +10,7 @@* Rob Herring <[email protected]> */ +#include <bcb.h> #include <common.h> #include <command.h> #include <env.h>
@@ -90,7 +91,16 @@ void fastboot_okay(const char *reason, char *response)*/ int __weak fastboot_set_reboot_flag (enum fastboot_reboot_reason reason) { -return -ENOSYS ; +static const char *const boot_cmds[] = { + [FASTBOOT_REBOOT_REASON_BOOTLOADER ] ="bootonce-bootloader" , + [FASTBOOT_REBOOT_REASON_FASTBOOTD ] ="boot-fastboot" , + [FASTBOOT_REBOOT_REASON_RECOVERY ] ="boot-recovery" + }; + +if (reason >=FASTBOOT_REBOOT_REASONS_COUNT ) +return -EINVAL ; + +return bcb_write_reboot_reason (CONFIG_FASTBOOT_FLASH_MMC_DEV ,"misc" , boot_cmds[reason]); }/**
Signature · 1 line
2.17.1