1 // SPDX-License-Identifier: GPL-2.0+
4 * based on: cmd_jffs2.c
6 * Add support for a CRAMFS located in RAM
19 #include <linux/list.h>
20 #include <linux/ctype.h>
21 #include <jffs2/jffs2.h>
22 #include <jffs2/load_kernel.h>
23 #include <cramfs/cramfs_fs.h>
26 /* enable/disable debugging messages */
31 # define DEBUGF(fmt, args...) printf(fmt ,##args)
33 # define DEBUGF(fmt, args...)
38 #ifndef CONFIG_MTD_NOR_FLASH
39 # define OFFSET_ADJUSTMENT 0
41 # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
44 #ifndef CONFIG_FS_JFFS2
45 #include <linux/stat.h>
46 char *mkmodestr(unsigned long mode, char *str)
48 static const char *l = "xwr";
52 switch (mode & S_IFMT) {
53 case S_IFDIR: str[0] = 'd'; break;
54 case S_IFBLK: str[0] = 'b'; break;
55 case S_IFCHR: str[0] = 'c'; break;
56 case S_IFIFO: str[0] = 'f'; break;
57 case S_IFLNK: str[0] = 'l'; break;
58 case S_IFSOCK: str[0] = 's'; break;
59 case S_IFREG: str[0] = '-'; break;
60 default: str[0] = '?';
63 for(i = 0; i < 9; i++) {
65 str[9-i] = (mode & mask)?c:'-';
69 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
70 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
71 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
75 #endif /* CONFIG_FS_JFFS2 */
77 extern int cramfs_check (struct part_info *info);
78 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
79 extern int cramfs_ls (struct part_info *info, char *filename);
80 extern int cramfs_info (struct part_info *info);
82 /***************************************************/
84 /***************************************************/
87 * Routine implementing fsload u-boot command. This routine tries to load
88 * a requested file from cramfs filesystem at location 'cramfsaddr'.
89 * cramfsaddr is an evironment variable.
91 * @param cmdtp command internal data
92 * @param flag command flag
93 * @param argc number of arguments supplied to the command
94 * @param argv arguments list
95 * @return 0 on success, 1 otherwise
97 int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
101 ulong offset = image_load_addr;
104 struct part_info part;
105 struct mtd_device dev;
109 addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
112 /* cramfs_* only supports NOR flash chips */
113 /* fake the device type */
114 id.type = MTD_DEV_TYPE_NOR;
118 /* fake the address offset */
119 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
121 /* pre-set Boot file name */
122 filename = env_get("bootfile");
130 offset = simple_strtoul(argv[1], NULL, 0);
131 image_load_addr = offset;
135 offset_virt = map_sysmem(offset, 0);
137 if (cramfs_check(&part))
138 size = cramfs_load (offset_virt, &part, filename);
141 printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
143 env_set_hex("filesize", size);
145 printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
148 unmap_sysmem(offset_virt);
149 unmap_sysmem((void *)(uintptr_t)part.offset);
155 * Routine implementing u-boot ls command which lists content of a given
156 * directory at location 'cramfsaddr'.
157 * cramfsaddr is an evironment variable.
159 * @param cmdtp command internal data
160 * @param flag command flag
161 * @param argc number of arguments supplied to the command
162 * @param argv arguments list
163 * @return 0 on success, 1 otherwise
165 int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
167 char *filename = "/";
169 struct part_info part;
170 struct mtd_device dev;
174 addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
177 /* cramfs_* only supports NOR flash chips */
178 /* fake the device type */
179 id.type = MTD_DEV_TYPE_NOR;
183 /* fake the address offset */
184 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
190 if (cramfs_check(&part))
191 ret = cramfs_ls (&part, filename);
192 unmap_sysmem((void *)(uintptr_t)part.offset);
197 /* command line only */
199 /***************************************************/
201 cramfsload, 3, 0, do_cramfs_load,
202 "load binary file from a filesystem image",
203 "[ off ] [ filename ]\n"
204 " - load binary file from address 'cramfsaddr'\n"
205 " with offset 'off'\n"
208 cramfsls, 2, 1, do_cramfs_ls,
209 "list files in a directory (default /)",
211 " - list files in a directory.\n"