[PATCH] Add flash_lock and flash_unlock applets.
Tito
farmatito at tiscali.it
Wed May 20 12:32:17 UTC 2009
On Wednesday 20 May 2009 09:46:34 Thierry Reding wrote:
> Signed-off-by: Thierry Reding <thierry.reding at avionic-design.de>
> ---
> include/applets.h | 2 +
> include/usage.h | 10 ++++++
> miscutils/Config.in | 14 ++++++++
> miscutils/Kbuild | 2 +
> miscutils/flash_lock.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++
> miscutils/flash_unlock.c | 56 ++++++++++++++++++++++++++++++++
> 6 files changed, 164 insertions(+), 0 deletions(-)
> create mode 100644 miscutils/flash_lock.c
> create mode 100644 miscutils/flash_unlock.c
>
> diff --git a/include/applets.h b/include/applets.h
> index 91f92e6..783ab5a 100644
> --- a/include/applets.h
> +++ b/include/applets.h
> @@ -155,6 +155,8 @@ IF_FDISK(APPLET(fdisk, _BB_DIR_SBIN, _BB_SUID_NEVER))
> IF_FEATURE_GREP_FGREP_ALIAS(APPLET_ODDNAME(fgrep, grep, _BB_DIR_BIN, _BB_SUID_NEVER, fgrep))
> IF_FIND(APPLET_NOEXEC(find, find, _BB_DIR_USR_BIN, _BB_SUID_NEVER, find))
> IF_FINDFS(APPLET(findfs, _BB_DIR_SBIN, _BB_SUID_MAYBE))
> +IF_FLASH_LOCK(APPLET(flash_lock, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
> +IF_FLASH_UNLOCK(APPLET(flash_unlock, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
> IF_FLASH_ERASEALL(APPLET(flash_eraseall, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
> IF_FOLD(APPLET(fold, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
> IF_FREE(APPLET(free, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
> diff --git a/include/usage.h b/include/usage.h
> index 1e327fb..45b2ac7 100644
> --- a/include/usage.h
> +++ b/include/usage.h
> @@ -1248,6 +1248,16 @@
> "$ find / -name passwd\n" \
> "/etc/passwd\n"
>
> +#define flash_lock_trivial_usage \
> + "MTD_DEVICE"
> +#define flash_lock_full_usage "\n\n" \
> + "lock an MTD device"
> +
> +#define flash_unlock_trivial_usage \
> + "MTD_DEVICE"
> +#define flash_unlock_full_usage "\n\n" \
> + "unlock an MTD device"
> +
> #define flash_eraseall_trivial_usage \
> "[-jq] MTD_DEVICE"
> #define flash_eraseall_full_usage "\n\n" \
> diff --git a/miscutils/Config.in b/miscutils/Config.in
> index 7feaf4a..06ff51a 100644
> --- a/miscutils/Config.in
> +++ b/miscutils/Config.in
> @@ -250,6 +250,20 @@ config FBSPLASH
> "NN" (ASCII decimal number) - percentage to show on progress bar
> "exit" - well you guessed it
>
> +config FLASH_LOCK
> + bool "flash_lock"
> + default n
> + help
> + The flash_lock binary from mtd-utils as of git head 5ec0c10d0. This
> + utility locks part or all of the flash device.
> +
> +config FLASH_UNLOCK
> + bool "flash_unlock"
> + default n
> + help
> + The flash_unlock binary from mtd-utils as of git head 5ec0c10d0. This
> + utility unlocks part or all of the flash device.
> +
> config FLASH_ERASEALL
> bool "flash_eraseall"
> default n
> diff --git a/miscutils/Kbuild b/miscutils/Kbuild
> index 23d7d8d..3098029 100644
> --- a/miscutils/Kbuild
> +++ b/miscutils/Kbuild
> @@ -16,6 +16,8 @@ lib-$(CONFIG_DEVFSD) += devfsd.o
> lib-$(CONFIG_DEVMEM) += devmem.o
> lib-$(CONFIG_EJECT) += eject.o
> lib-$(CONFIG_FBSPLASH) += fbsplash.o
> +lib-$(CONFIG_FLASH_LOCK) += flash_lock.o
> +lib-$(CONFIG_FLASH_UNLOCK) += flash_unlock.o
> lib-$(CONFIG_FLASH_ERASEALL) += flash_eraseall.o
> lib-$(CONFIG_IONICE) += ionice.o
> lib-$(CONFIG_HDPARM) += hdparm.o
> diff --git a/miscutils/flash_lock.c b/miscutils/flash_lock.c
> new file mode 100644
> index 0000000..ae7ec22
> --- /dev/null
> +++ b/miscutils/flash_lock.c
> @@ -0,0 +1,80 @@
> +/*
> + * miscutils/flash_lock.c
> + *
> + * Ported to busybox from mtd-utils.
> + *
> + * This utility locks one or more sectors of flash device.
> + *
> + */
> +
> +#include "libbb.h"
> +#include <mtd/mtd-user.h>
> +
> +int flash_lock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
> +int flash_lock_main(int argc, char **argv)
> +{
> + int fd;
> + struct mtd_info_user info;
> + struct erase_info_user lock;
> + int num_sectors;
> + int ofs;
> +
> + /*
> + * Parse command line options
> + */
> + if (argc != 4) {
Please use show_usage();
> + fprintf(stderr, "usage: %s MTD_DEVICE OFFSET SECTORS\n",
> + argv[0]);
> + return EXIT_FAILURE;
> + } else if (strncmp(argv[1], "/dev/mtd", 8) != 0) {
Please use bb_error_ms_and_die()
> + fprintf(stderr, "'%s' is not a MTD device. Must specify mtd "
> + "device: /dev/mtd?\n", argv[1]);
> + return EXIT_FAILURE;
> + }
> +
> + fd = xopen(argv[1], O_RDWR);
Remove this, xopen already prints an error message and exits so this is dead code
> + if (fd < 0) {
> + fprintf(stderr, "Could not open mtd device: %s\n", argv[1]);
> + return EXIT_FAILURE;
> + }
> +
Please use xioctl()
> + if (ioctl(fd, MEMGETINFO, &info)) {
> + fprintf(stderr, "Could not get MTD device info from %s\n",
> + argv[1]);
> + close(fd);
> + return EXIT_FAILURE;
> + }
> +
Some data validation here would be good
as this is untrusted user input, see libbb/xatonum.c
Could ofs and num_sectors be used uninitialized here
if user supplies wrong data?
> + sscanf(argv[2], "%x",&ofs);
> + sscanf(argv[3], "%d",&num_sectors);
> +
> + if (ofs > (info.size - info.erasesize)) {
> + unsigned int size = info.size - info.erasesize;
Please use bb_error_msg_and_die
> + fprintf(stderr, "%x is beyond device size %x\n", ofs, size);
> + return EXIT_FAILURE;
> + }
> +
> + if (num_sectors == -1) {
> + num_sectors = info.size / info.erasesize;
> + } else {
> + if (num_sectors > (info.size / info.erasesize)) {
> + int sectors = info.size / info.erasesize;
Please use bb_error_msg_and_die
> + fprintf(stderr, "%d are too many sectors, device "
> + "only has %d\n", num_sectors,
> + sectors);
> + return EXIT_FAILURE;
> + }
> + }
> +
> + lock.start = ofs;
> + lock.length = num_sectors * info.erasesize;
> +
Please use xioctl()
> + if (ioctl(fd, MEMLOCK, &lock)) {
> + fprintf(stderr, "Could not lock MTD device: %s\n", argv[1]);
> + close(fd);
> + return EXIT_FAILURE;
> + }
> +
> + return EXIT_SUCCESS;
> +}
> +
> diff --git a/miscutils/flash_unlock.c b/miscutils/flash_unlock.c
> new file mode 100644
> index 0000000..a0a6f59
> --- /dev/null
> +++ b/miscutils/flash_unlock.c
> @@ -0,0 +1,56 @@
> +/*
> + * miscutils/flash_unlock.c
> + *
> + * Ported to busybox from mtd-utils.
> + *
> + * This utility unlock all sectors of flash device.
> + *
> + */
> +
> +#include "libbb.h"
> +#include <mtd/mtd-user.h>
> +
> +int flash_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
> +int flash_unlock_main(int argc UNUSED_PARAM, char **argv)
> +{
> + int fd;
> + struct mtd_info_user info;
> + struct erase_info_user lock;
> +
> + /*
> + * Parse command line options
> + */
> + if (argc != 2) {
Please use show_usage();
> + fprintf(stderr, "usage: %s MTD_DEVICE\n", argv[0]);
> + return EXIT_FAILURE;
> + } else if (strncmp(argv[1], "/dev/mtd", 8) != 0) {
Please use bb_error_msg_and_die
> + fprintf(stderr, "'%s' is not a MTD device. Must specify "
> + "mtd device: /dev/mtd?\n", argv[1]);
> + return EXIT_FAILURE;
> + }
> +
> + fd = xopen(argv[1], O_RDWR);
Remove this, xopen already prints an error message and exits so this is dead code
> + if (fd < 0) {
> + fprintf(stderr, "Could not open mtd device: %s\n", argv[1]);
> + return EXIT_FAILURE;
> + }
> +
Please use xioctl()
> + if (ioctl(fd, MEMGETINFO, &info)) {
> + fprintf(stderr, "Could not get MTD device info from %s\n",
> + argv[1]);
> + close(fd);
> + return EXIT_FAILURE;
> + }
> +
> + lock.start = 0;
> + lock.length = info.size;
> +
Please use xioctl()
> + if (ioctl(fd, MEMUNLOCK, &lock)) {
> + fprintf(stderr, "Could not unlock MTD device: %s\n", argv[1]);
> + close(fd);
> + return EXIT_FAILURE;
> + }
> +
> + return EXIT_SUCCESS;
> +}
> +
Hi,
please use libbb functions as shown above.
It seems to me that due to the fact that the applets share
a lot of code they could be merged into one applet and at runtime
you can check:
if (applet_name[6] == 'u') {
/* do flash unlock stuff */
}else {
/* do flash lock stuff */
}
Ciao,
Tito
More information about the busybox
mailing list