implement ping using new network stack
[barebox-mini2440.git] / commands / flash.c
blob20f5cfc33a5c37fb40ec94e76818cc6adace0d08
1 /*
2 * erase, protect, unprotect - FLASH support
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
8 * project.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
26 /**
27 * @file
28 * @brief Flash memory support: erase, protect, unprotect
31 #include <common.h>
32 #include <command.h>
33 #include <errno.h>
34 #include <getopt.h>
35 #include <fs.h>
36 #include <fcntl.h>
37 #include <linux/stat.h>
39 static int do_flerase(struct command *cmdtp, int argc, char *argv[])
41 int fd;
42 char *filename = NULL;
43 struct stat s;
44 unsigned long start = 0, size = ~0;
45 int ret = 0;
47 if (argc == 1)
48 return COMMAND_ERROR_USAGE;
50 filename = argv[1];
52 if (stat(filename, &s)) {
53 printf("stat %s: %s\n", filename, errno_str());
54 return 1;
57 size = s.st_size;
59 if (!filename) {
60 printf("missing filename\n");
61 return 1;
64 fd = open(filename, O_WRONLY);
65 if (fd < 0) {
66 printf("open %s:", filename, errno_str());
67 return 1;
70 if (argc == 3 && parse_area_spec(argv[2], &start, &size)) {
71 printf("could not parse: %s\n", argv[optind]);
72 ret = 1;
73 goto out;
76 if (erase(fd, size, start)) {
77 perror("erase");
78 ret = 1;
80 out:
81 close(fd);
83 return ret;
86 static const __maybe_unused char cmd_erase_help[] =
87 "Usage: erase <device> [area]\n"
88 "Erase a flash device or parts of a device if an area specification\n"
89 "is given\n";
91 BAREBOX_CMD_START(erase)
92 .cmd = do_flerase,
93 .usage = "erase FLASH memory",
94 BAREBOX_CMD_HELP(cmd_erase_help)
95 BAREBOX_CMD_END
97 /** @page erase_command erase Erase flash memory
99 * Usage is: erase \<devicee>
101 * Erase the flash memory behind the device. It depends on the device given,
102 * what area will be erased. If the device represents the whole flash memory
103 * the whole memory will be erased. If the device represents a partition on
104 * a main flash memory, only this partition part will be erased.
106 * Refer \b addpart, \b delpart and \b devinfo for partition handling.
109 static int do_protect(struct command *cmdtp, int argc, char *argv[])
111 int fd;
112 char *filename = NULL;
113 struct stat s;
114 int prot = 1;
115 unsigned long start = 0, size = ~0;
116 int ret = 0, err;
118 if (argc == 1)
119 return COMMAND_ERROR_USAGE;
121 filename = argv[1];
123 if (*argv[0] == 'u')
124 prot = 0;
126 if (stat(filename, &s)) {
127 printf("stat %s: %s\n", filename, errno_str());
128 return 1;
131 size = s.st_size;
133 if (!filename) {
134 printf("missing filename\n");
135 return 1;
138 fd = open(filename, O_WRONLY);
139 if (fd < 0) {
140 printf("open %s:", filename, errno_str());
141 return 1;
144 if (argc == 3)
145 if (parse_area_spec(argv[2], &start, &size)) {
146 printf("could not parse: %s\n", argv[optind]);
147 ret = 1;
148 goto out;
151 err = protect(fd, size, start, prot);
152 if (err && err != -ENOSYS) {
153 perror("protect");
154 ret = 1;
155 goto out;
157 out:
158 close(fd);
160 return ret;
163 static const __maybe_unused char cmd_protect_help[] =
164 "Usage: (un)protect <device> [area]\n"
165 "(un)protect a flash device or parts of a device if an area specification\n"
166 "is given\n";
168 BAREBOX_CMD_START(protect)
169 .cmd = do_protect,
170 .usage = "enable FLASH write protection",
171 BAREBOX_CMD_HELP(cmd_protect_help)
172 BAREBOX_CMD_END
174 BAREBOX_CMD_START(unprotect)
175 .cmd = do_protect,
176 .usage = "disable FLASH write protection",
177 BAREBOX_CMD_HELP(cmd_protect_help)
178 BAREBOX_CMD_END
180 /** @page protect_command protect Protect a flash memory
182 * Usage is: protect \<devicee>
184 * Protect the flash memory behind the device. It depends on the device given,
185 * what area will be protected. If the device represents the whole flash memory
186 * the whole memory will be protected. If the device represents a partition on
187 * a main flash memory, only this partition part will be protected.
189 * Refer \b addpart, \b delpart and \b devinfo for partition handling.
192 /** @page unprotect_command unprotect Unprotect a flash memory
194 * Usage is: unprotect \<devicee>
196 * Unprotect the flash memory behind the device. It depends on the device given,
197 * what area will be unprotected. If the device represents the whole flash memory
198 * the whole memory will be unprotected. If the device represents a partition
199 * on a main flash memory, only this partition part will be unprotected.
201 * Refer \b addpart, \b delpart and \b devinfo for partition handling.