1 // SPDX-License-Identifier: GPL-2.0+
4 * based on: cmd_jffs2.c
6 * Add support for a CRAMFS located in RAM
18 #include <linux/list.h>
19 #include <linux/ctype.h>
20 #include <jffs2/jffs2.h>
21 #include <jffs2/load_kernel.h>
22 #include <cramfs/cramfs_fs.h>
25 /* enable/disable debugging messages */
30 # define DEBUGF(fmt, args...) printf(fmt ,##args)
32 # define DEBUGF(fmt, args...)
37 #ifndef CONFIG_MTD_NOR_FLASH
38 # define OFFSET_ADJUSTMENT 0
40 # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
43 #ifndef CONFIG_FS_JFFS2
44 #include <linux/stat.h>
45 char *mkmodestr(unsigned long mode, char *str)
47 static const char *l = "xwr";
51 switch (mode & S_IFMT) {
52 case S_IFDIR: str[0] = 'd'; break;
53 case S_IFBLK: str[0] = 'b'; break;
54 case S_IFCHR: str[0] = 'c'; break;
55 case S_IFIFO: str[0] = 'f'; break;
56 case S_IFLNK: str[0] = 'l'; break;
57 case S_IFSOCK: str[0] = 's'; break;
58 case S_IFREG: str[0] = '-'; break;
59 default: str[0] = '?';
62 for(i = 0; i < 9; i++) {
64 str[9-i] = (mode & mask)?c:'-';
68 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
69 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
70 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
74 #endif /* CONFIG_FS_JFFS2 */
76 extern int cramfs_check (struct part_info *info);
77 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
78 extern int cramfs_ls (struct part_info *info, char *filename);
79 extern int cramfs_info (struct part_info *info);
81 /***************************************************/
83 /***************************************************/
86 * Routine implementing fsload u-boot command. This routine tries to load
87 * a requested file from cramfs filesystem at location 'cramfsaddr'.
88 * cramfsaddr is an evironment variable.
90 * @param cmdtp command internal data
91 * @param flag command flag
92 * @param argc number of arguments supplied to the command
93 * @param argv arguments list
94 * @return 0 on success, 1 otherwise
96 int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
100 ulong offset = load_addr;
103 struct part_info part;
104 struct mtd_device dev;
108 addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
111 /* cramfs_* only supports NOR flash chips */
112 /* fake the device type */
113 id.type = MTD_DEV_TYPE_NOR;
117 /* fake the address offset */
118 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
120 /* pre-set Boot file name */
121 filename = env_get("bootfile");
129 offset = simple_strtoul(argv[1], NULL, 0);
134 offset_virt = map_sysmem(offset, 0);
136 if (cramfs_check(&part))
137 size = cramfs_load (offset_virt, &part, filename);
140 printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
142 env_set_hex("filesize", size);
144 printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
147 unmap_sysmem(offset_virt);
148 unmap_sysmem((void *)(uintptr_t)part.offset);
154 * Routine implementing u-boot ls command which lists content of a given
155 * directory at location 'cramfsaddr'.
156 * cramfsaddr is an evironment variable.
158 * @param cmdtp command internal data
159 * @param flag command flag
160 * @param argc number of arguments supplied to the command
161 * @param argv arguments list
162 * @return 0 on success, 1 otherwise
164 int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
166 char *filename = "/";
168 struct part_info part;
169 struct mtd_device dev;
173 addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
176 /* cramfs_* only supports NOR flash chips */
177 /* fake the device type */
178 id.type = MTD_DEV_TYPE_NOR;
182 /* fake the address offset */
183 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
189 if (cramfs_check(&part))
190 ret = cramfs_ls (&part, filename);
191 unmap_sysmem((void *)(uintptr_t)part.offset);
196 /* command line only */
198 /***************************************************/
200 cramfsload, 3, 0, do_cramfs_load,
201 "load binary file from a filesystem image",
202 "[ off ] [ filename ]\n"
203 " - load binary file from address 'cramfsaddr'\n"
204 " with offset 'off'\n"
207 cramfsls, 2, 1, do_cramfs_ls,
208 "list files in a directory (default /)",
210 " - list files in a directory.\n"