1 //==========================================================================
5 // RedBoot fileio support
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 // Copyright (C) 2002, 2003, 2004 Gary Thomas
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
44 // Author(s): dwmw2, msalter
49 // This code is part of RedBoot (tm).
51 //####DESCRIPTIONEND####
53 //==========================================================================
55 // Shoot me. But I don't want struct timeval because redboot provides it.
66 #ifdef CYGPKG_IO_FLASH
67 #include <cyg/io/io.h>
68 #include <cyg/io/flash.h>
69 #include <cyg/io/config_keys.h>
71 #include <cyg/fileio/fileio.h>
72 #include <cyg/infra/cyg_ass.h> // assertion macros
74 static void do_mount(int argc, char *argv[]);
75 static void do_umount(int argc, char *argv[]);
77 #ifdef CYGPKG_IO_FLASH_BLOCK_DEVICE
78 #define FLASHPART "[-f <partition>] "
85 FLASHPART "[-d <device>] -t fstype",
89 "Unmount file system",
94 int fileio_mounted = 0;
96 // Mount disk/filesystem
98 do_mount(int argc, char *argv[])
100 char *part_str, *dev_str, *type_str;
101 bool part_set = false, dev_set = false, type_set = false;
102 struct option_info opts[3];
103 int err, num_opts = 2;
105 init_opts(&opts[0], 'd', true, OPTION_ARG_TYPE_STR,
106 (void *)&dev_str, &dev_set, "device");
107 init_opts(&opts[1], 't', true, OPTION_ARG_TYPE_STR,
108 (void *)&type_str, &type_set, "fstype");
109 #ifdef CYGPKG_IO_FLASH_BLOCK_DEVICE
110 init_opts(&opts[2], 'f', true, OPTION_ARG_TYPE_STR,
111 (void *)&part_str, &part_set, "partition");
115 CYG_ASSERT(num_opts <= NUM_ELEMS(opts), "Too many options");
117 if (!scan_opts(argc, argv, 1, opts, num_opts, NULL, 0, NULL))
121 diag_printf("Must specify file system type\n");
124 if (fileio_mounted) {
125 diag_printf("A file system is already mounted\n");
128 #ifdef CYGPKG_IO_FLASH_BLOCK_DEVICE
133 if (dev_set && strcmp(dev_str, CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1)) {
134 diag_printf("May only set one of <device> or <partition>\n");
138 dev_str = CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1;
139 len = strlen(part_str);
141 err = cyg_io_lookup(dev_str, &h);
143 diag_printf("cyg_io_lookup of \"%s\" returned %d\n", err);
146 err = cyg_io_set_config(h, CYG_IO_SET_CONFIG_FLASH_FIS_NAME,
149 diag_printf("FIS partition \"%s\" not found\n",
155 err = mount(dev_str, "/", type_str);
158 diag_printf("Mount failed %d\n", err);
160 // diag_printf("Mount %s file system succeeded\n", type_str);
162 #ifdef CYGBLD_REDBOOT_FILEIO_WITH_LS
169 do_umount(int argc, char *argv[])
171 if (!fileio_mounted) {
178 #ifdef CYGBLD_REDBOOT_FILEIO_WITH_LS
181 static char rwx[8][4] = { "---", "r--", "-w-", "rw-", "--x", "r-x", "-wx", "rwx" };
184 do_ls(int argc, char * argv[])
187 struct option_info opts[1];
188 bool dir_set = false;
191 char filename[PATH_MAX];
195 init_opts(&opts[0], 'd', true, OPTION_ARG_TYPE_STR,
196 (void *)&dir_str, &dir_set, "directory");
198 if (!fileio_mounted) {
199 diag_printf("No filesystem mounted\n");
203 if (!scan_opts(argc, argv, 1, opts, 1, NULL, 0, NULL))
207 getcwd(cwd,sizeof(cwd));
211 diag_printf("directory %s\n",dir_str);
212 dirp = opendir(dir_str);
214 diag_printf("no such directory %s\n",dir_str);
219 struct dirent *entry = readdir(dirp);
224 strcpy(filename, dir_str);
225 strcat(filename, "/");
226 strcat(filename, entry->d_name);
228 err = stat(filename, &sbuf);
230 diag_printf("Unable to stat file %s\n", filename);
233 diag_printf("%4d ", sbuf.st_ino);
234 if (S_ISDIR(sbuf.st_mode)) diag_printf("d");
235 if (S_ISCHR(sbuf.st_mode)) diag_printf("c");
236 if (S_ISBLK(sbuf.st_mode)) diag_printf("b");
237 if (S_ISREG(sbuf.st_mode)) diag_printf("-");
238 if (S_ISLNK(sbuf.st_mode)) diag_printf("l");
239 diag_printf("%s%s%s", // Ho, humm, have to hard code the shifts
240 rwx[(sbuf.st_mode & S_IRWXU) >> 16],
241 rwx[(sbuf.st_mode & S_IRWXG) >> 19],
242 rwx[(sbuf.st_mode & S_IRWXO) >> 22]);
243 diag_printf(" %2d size %6d %s\n",
244 sbuf.st_nlink,sbuf.st_size,
253 "list directory contents",
258 #endif // CYGBLD_REDBOOT_FILEIO_WITH_LS
262 fileio_stream_open(connection_info_t *info, int *err)
264 char *filename = info->filename;
266 if (!fileio_mounted) {
267 diag_printf("No file system mounted\n");
270 fd = open(filename, O_RDONLY);
272 diag_printf("Open failed, error %d\n", errno);
279 fileio_stream_read(char *buf, int size, int *err)
283 if ((nread = read(fd, buf, size)) < 0) {
291 fileio_stream_close(int *err)
297 fileio_error(int err)
299 static char myerr[10];
301 diag_sprintf(myerr, "error %d\n", err);
308 GETC_IO_FUNCS(fileio_io, fileio_stream_open, fileio_stream_close,
309 0, fileio_stream_read, fileio_error);
310 RedBoot_load(file, fileio_io, true, true, 0);