Merge tag 'xilinx-for-v2025.04-rc2' of https://source.denx.de/u-boot/custodians/u...
[u-boot.git] / cmd / cramfs.c
blobbaff50d1bd686e637e1241f21922137dc80f4358
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
4 * based on: cmd_jffs2.c
6 * Add support for a CRAMFS located in RAM
7 */
9 /*
10 * CRAMFS support
12 #include <command.h>
13 #include <env.h>
14 #include <image.h>
15 #include <malloc.h>
16 #include <mapmem.h>
17 #include <linux/list.h>
18 #include <linux/ctype.h>
19 #include <jffs2/jffs2.h>
20 #include <jffs2/load_kernel.h>
21 #include <cramfs/cramfs_fs.h>
22 #include <asm/io.h>
24 /* enable/disable debugging messages */
25 #define DEBUG_CRAMFS
26 #undef DEBUG_CRAMFS
28 #ifdef DEBUG_CRAMFS
29 # define DEBUGF(fmt, args...) printf(fmt ,##args)
30 #else
31 # define DEBUGF(fmt, args...)
32 #endif
34 #ifndef CONFIG_MTD_NOR_FLASH
35 # define OFFSET_ADJUSTMENT 0
36 #else
37 #include <flash.h>
38 # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
39 #endif
41 #ifndef CONFIG_FS_JFFS2
42 #include <linux/stat.h>
43 char *mkmodestr(unsigned long mode, char *str)
45 static const char *l = "xwr";
46 int mask = 1, i;
47 char c;
49 switch (mode & S_IFMT) {
50 case S_IFDIR: str[0] = 'd'; break;
51 case S_IFBLK: str[0] = 'b'; break;
52 case S_IFCHR: str[0] = 'c'; break;
53 case S_IFIFO: str[0] = 'f'; break;
54 case S_IFLNK: str[0] = 'l'; break;
55 case S_IFSOCK: str[0] = 's'; break;
56 case S_IFREG: str[0] = '-'; break;
57 default: str[0] = '?';
60 for(i = 0; i < 9; i++) {
61 c = l[i%3];
62 str[9-i] = (mode & mask)?c:'-';
63 mask = mask<<1;
66 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
67 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
68 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
69 str[10] = '\0';
70 return str;
72 #endif /* CONFIG_FS_JFFS2 */
74 extern int cramfs_check (struct part_info *info);
75 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
76 extern int cramfs_ls (struct part_info *info, char *filename);
77 extern int cramfs_info (struct part_info *info);
79 /***************************************************/
80 /* U-Boot commands */
81 /***************************************************/
83 /**
84 * Routine implementing fsload u-boot command. This routine tries to load
85 * a requested file from cramfs filesystem at location 'cramfsaddr'.
86 * cramfsaddr is an evironment variable.
88 * @param cmdtp command internal data
89 * @param flag command flag
90 * @param argc number of arguments supplied to the command
91 * @param argv arguments list
92 * Return: 0 on success, 1 otherwise
94 int do_cramfs_load(struct cmd_tbl *cmdtp, int flag, int argc,
95 char *const argv[])
97 char *filename;
98 int size;
99 ulong offset = image_load_addr;
100 char *offset_virt;
102 struct part_info part;
103 struct mtd_device dev;
104 struct mtdids id;
106 ulong addr;
107 addr = hextoul(env_get("cramfsaddr"), NULL);
109 /* hack! */
110 /* cramfs_* only supports NOR flash chips */
111 /* fake the device type */
112 id.type = MTD_DEV_TYPE_NOR;
113 id.num = 0;
114 dev.id = &id;
115 part.dev = &dev;
116 /* fake the address offset */
117 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
119 /* pre-set Boot file name */
120 filename = env_get("bootfile");
121 if (!filename)
122 filename = "uImage";
124 if (argc == 2) {
125 filename = argv[1];
127 if (argc == 3) {
128 offset = simple_strtoul(argv[1], NULL, 0);
129 image_load_addr = offset;
130 filename = argv[2];
133 offset_virt = map_sysmem(offset, 0);
134 size = 0;
135 if (cramfs_check(&part))
136 size = cramfs_load (offset_virt, &part, filename);
138 if (size > 0) {
139 printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
140 size, offset);
141 env_set_hex("filesize", size);
142 } else {
143 printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
146 unmap_sysmem(offset_virt);
147 unmap_sysmem((void *)(uintptr_t)part.offset);
149 return !(size > 0);
153 * Routine implementing u-boot ls command which lists content of a given
154 * directory at location 'cramfsaddr'.
155 * cramfsaddr is an evironment variable.
157 * @param cmdtp command internal data
158 * @param flag command flag
159 * @param argc number of arguments supplied to the command
160 * @param argv arguments list
161 * Return: 0 on success, 1 otherwise
163 int do_cramfs_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
165 char *filename = "/";
166 int ret;
167 struct part_info part;
168 struct mtd_device dev;
169 struct mtdids id;
171 ulong addr;
172 addr = hextoul(env_get("cramfsaddr"), NULL);
174 /* hack! */
175 /* cramfs_* only supports NOR flash chips */
176 /* fake the device type */
177 id.type = MTD_DEV_TYPE_NOR;
178 id.num = 0;
179 dev.id = &id;
180 part.dev = &dev;
181 /* fake the address offset */
182 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
184 if (argc == 2)
185 filename = argv[1];
187 ret = 0;
188 if (cramfs_check(&part))
189 ret = cramfs_ls (&part, filename);
190 unmap_sysmem((void *)(uintptr_t)part.offset);
192 return ret ? 0 : 1;
195 /* command line only */
197 /***************************************************/
198 U_BOOT_CMD(
199 cramfsload, 3, 0, do_cramfs_load,
200 "load binary file from a filesystem image",
201 "[ off ] [ filename ]\n"
202 " - load binary file from address 'cramfsaddr'\n"
203 " with offset 'off'\n"
205 U_BOOT_CMD(
206 cramfsls, 2, 1, do_cramfs_ls,
207 "list files in a directory (default /)",
208 "[ directory ]\n"
209 " - list files in a directory.\n"