[PATCH 2/2] Add flash_lock and flash_unlock applets.

Tito farmatito at tiscali.it
Fri May 22 11:52:07 UTC 2009


On Friday 22 May 2009 11:08:40 Thierry Reding wrote:
> Signed-off-by: Thierry Reding <thierry.reding at avionic-design.de>
> ---
>  include/applets.h             |    2 +
>  include/usage.h               |   11 ++++++
>  miscutils/Config.in           |   20 ++++++++++++
>  miscutils/Kbuild              |    1 +
>  miscutils/flash_lock_unlock.c |   70 +++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 104 insertions(+), 0 deletions(-)
>  create mode 100644 miscutils/flash_lock_unlock.c
> 
> diff --git a/include/applets.h b/include/applets.h
> index 7838757..8ce81a2 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_ODDNAME(flash_lock, flash_lock_unlock, _BB_DIR_USR_SBIN, _BB_SUID_NEVER, flash_lock))
> +IF_FLASH_UNLOCK(APPLET_ODDNAME(flash_unlock, flash_lock_unlock, _BB_DIR_USR_SBIN, _BB_SUID_NEVER, flash_unlock))
>  //IF_FLASH_ERASEALL(APPLET_ODDNAME(flash_eraseall, flash_eraseall, _BB_DIR_USR_SBIN, _BB_SUID_NEVER, flash_eraseall))
>  IF_FLASH_ERASEALL(APPLET(flash_eraseall, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
>  IF_FOLD(APPLET(fold, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
> diff --git a/include/usage.h b/include/usage.h
> index 1e327fb..a496429 100644
> --- a/include/usage.h
> +++ b/include/usage.h
> @@ -1248,6 +1248,17 @@
>         "$ find / -name passwd\n" \
>         "/etc/passwd\n"
>  
> +#define flash_lock_trivial_usage \
> +       "MTD_DEVICE OFFSET SECTORS"
> +#define flash_lock_full_usage "\n\n" \
> +       "Lock part or all of an MTD device. If SECTORS is -1, then all sectors will\n" \
> +       "be locked, regardless of the value of OFFSET."
> +
> +#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..0f0a679 100644
> --- a/miscutils/Config.in
> +++ b/miscutils/Config.in
> @@ -250,6 +250,26 @@ config FBSPLASH
>  	    "NN" (ASCII decimal number) - percentage to show on progress bar
>  	    "exit" - well you guessed it
>  
> +config FLASH_LOCK_UNLOCK
> +	bool
> +	default n
> +
> +config FLASH_LOCK
> +	bool "flash_lock"
> +	select FLASH_LOCK_UNLOCK
> +	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"
> +	select FLASH_LOCK_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..d155ae0 100644
> --- a/miscutils/Kbuild
> +++ b/miscutils/Kbuild
> @@ -16,6 +16,7 @@ lib-$(CONFIG_DEVFSD)      += devfsd.o
>  lib-$(CONFIG_DEVMEM)      += devmem.o
>  lib-$(CONFIG_EJECT)       += eject.o
>  lib-$(CONFIG_FBSPLASH)    += fbsplash.o
> +lib-$(CONFIG_FLASH_LOCK_UNLOCK)	+= flash_lock_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_unlock.c b/miscutils/flash_lock_unlock.c
> new file mode 100644
> index 0000000..226932e
> --- /dev/null
> +++ b/miscutils/flash_lock_unlock.c
> @@ -0,0 +1,70 @@
> +#include "libbb.h"
> +#include <mtd/mtd-user.h>
> +
> +int flash_lock_unlock_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
> +int flash_lock_unlock_main(int argc, char *argv[])
> +{
> +	struct mtd_info_user info;
> +	struct erase_info_user lock;
> +	unsigned long offset = 0;
> +	long sectors = -1;
> +	int do_lock = 1;
> +	int fd;
> +
> +	if (strcmp(argv[0], "flash_unlock") == 0)
> +		do_lock = 0;
> +
> +	/* check number of command-line arguments */
> +	if ((do_lock && (argc != 4)) || (!do_lock && (argc != 2)))
> +		bb_show_usage();
> +
> +	/* parse offset and number of sectors to lock */
> +	if (do_lock) {
> +		offset = xstrtoul(argv[2], 0);
> +		sectors = xstrtol(argv[3], 0);
> +	}
> +
> +	if (strncmp(argv[1], "/dev/mtd", 8) != 0) {
> +		bb_error_msg_and_die("'%s' is not an MTD device. Must specify "
> +				"an MTD devic: /dev/mtd?\n", argv[1]);
> +	}
> +
> +	fd = xopen(argv[1], O_RDWR);
> +
> +	if (xioctl(fd, MEMGETINFO, &info)) {
> +		bb_error_msg_and_die("failed to get MTD device info from %s\n",
> +				argv[1]);
> +	}
> +
> +	if (do_lock) {
> +		if (offset > (info.size - info.erasesize)) {
> +			unsigned long size = info.size - info.erasesize;
> +			bb_error_msg_and_die("%lx is beyond device size %lx\n",
> +					offset, size);
> +		}
> +
> +		if (sectors == -1) {
> +			sectors = info.size / info.erasesize;
> +		} else {
> +			if (sectors > (info.size / info.erasesize)) {
> +				unsigned long num = info.size / info.erasesize;
> +				bb_error_msg_and_die("%ld are too many "
> +						"sectors, device only has "
> +						"%ld\n", sectors, num);
> +			}
> +		}
> +
> +		lock.start = offset;
> +		lock.length = sectors * info.erasesize;
> +	} else {
> +		lock.start = 0;
> +		lock.length = info.size;
> +	}
> +
> +	if (xioctl(fd, do_lock ? MEMLOCK : MEMUNLOCK, &lock)) {
> +		bb_error_msg_and_die("failed to %s MTD device %s\n",
> +				do_lock ? "lock" : "unlock", argv[1]);
> +	}
> +
> +	return EXIT_SUCCESS;
> +}

Hi,
I suggest a size reduced version, this is not tested nor compiled,
due to lack of time. 

Ciao,
Tito

int flash_lock_unlock_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
int flash_lock_unlock_main(int argc, char *argv[])
{
       struct mtd_info_user info;
       struct erase_info_user lock;
       unsigned long offset = 0;
       long sectors = -1;
       int do_lock = 1;
       int fd;

       if (applet_name[6] == 'u')
               do_lock = 0;

       /* check number of command-line arguments */
       if ((do_lock && (argc != 4)) || (!do_lock && (argc != 2)))
               bb_show_usage();

       if (strncmp(argv[1], "/dev/mtd", 8) != 0) {
               bb_error_msg_and_die("'%s' is not an MTD device", argv[1]);
       }

       fd = xopen(argv[1], O_RDWR);

       xioctl(fd, MEMGETINFO, &info);
  
       if (do_lock) {
               /* parse offset and number of sectors to lock */
               offset = xstrtoul_range(argv[2], 10, 0, info.size - info.erasesize);
               sectors = xstrtol(argv[3], 0);
	       // sectors cannot be -1 here
               //if (sectors == -1) {
               //        sectors = info.size / info.erasesize;
               //}
               lock.start = offset;
               lock.length = sectors * info.erasesize;
       } else {
               lock.start = 0;
               lock.length = info.size;
       }

       xioctl(fd, do_lock ? MEMLOCK : MEMUNLOCK, &lock);
       return EXIT_SUCCESS;
}



More information about the busybox mailing list