1/* vi: set sw=4 ts=4: */
2/* Ported to busybox from mtd-utils.
3 *
4 * Licensed under GPLv2, see file LICENSE in this tarball for details.
5 */
6#include "libbb.h"
7#include <mtd/mtd-user.h>
8
9int flash_lock_unlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
10int flash_lock_unlock_main(int argc UNUSED_PARAM, char **argv)
11{
12	/* note: fields in these structs are 32-bits.
13	 * apparently we can't win anything by using off_t
14	 * or long long's for offset and/or sectors vars. */
15	struct mtd_info_user info;
16	struct erase_info_user lock;
17	unsigned long offset;
18	long sectors;
19	int fd;
20
21#define do_lock (ENABLE_FLASH_LOCK && (!ENABLE_FLASH_UNLOCK || (applet_name[6] == 'l')))
22
23	if (!argv[1])
24		bb_show_usage();
25
26	/* parse offset and number of sectors to lock */
27	offset = 0;
28	sectors = -1;
29	if (do_lock) {
30		if (!argv[2] || !argv[3])
31			bb_show_usage();
32		offset = xstrtoul(argv[2], 0);
33		sectors = xstrtol(argv[3], 0);
34	}
35
36	fd = xopen(argv[1], O_RDWR);
37
38	xioctl(fd, MEMGETINFO, &info);
39
40	lock.start = 0;
41	lock.length = info.size;
42	if (do_lock) {
43		unsigned long size = info.size - info.erasesize;
44		if (offset > size) {
45			bb_error_msg_and_die("%lx is beyond device size %lx\n",
46					offset, size);
47		}
48
49		if (sectors == -1) {
50			sectors = info.size / info.erasesize;
51		} else {
52// isn't this useless?
53			unsigned long num = info.size / info.erasesize;
54			if (sectors > num) {
55				bb_error_msg_and_die("%ld are too many "
56						"sectors, device only has "
57						"%ld\n", sectors, num);
58			}
59		}
60
61		lock.start = offset;
62		lock.length = sectors * info.erasesize;
63		xioctl(fd, MEMLOCK, &lock);
64	} else {
65		xioctl(fd, MEMUNLOCK, &lock);
66	}
67
68	return EXIT_SUCCESS;
69}
70