blob: ad232deae009c2b323b85f5a185a393da5316adf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher07b46272010-01-07 08:55:54 +01002/*
Heiko Schocher07b46272010-01-07 08:55:54 +01003 *
4 * based on: cmd_jffs2.c
5 *
6 * Add support for a CRAMFS located in RAM
7 */
8
9
10/*
11 * CRAMFS support
12 */
13#include <common.h>
14#include <command.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070016#include <image.h>
Heiko Schocher07b46272010-01-07 08:55:54 +010017#include <malloc.h>
Tyler Hall59417d02017-04-12 16:29:15 -040018#include <mapmem.h>
Heiko Schocher07b46272010-01-07 08:55:54 +010019#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>
Tyler Hall59417d02017-04-12 16:29:15 -040024#include <asm/io.h>
Heiko Schocher07b46272010-01-07 08:55:54 +010025
26/* enable/disable debugging messages */
27#define DEBUG_CRAMFS
28#undef DEBUG_CRAMFS
29
30#ifdef DEBUG_CRAMFS
31# define DEBUGF(fmt, args...) printf(fmt ,##args)
32#else
33# define DEBUGF(fmt, args...)
34#endif
35
Heiko Schocher1bef5ca2011-05-03 02:15:01 +000036#include <flash.h>
Heiko Schocher07b46272010-01-07 08:55:54 +010037
Masahiro Yamada8cea9b52017-02-11 22:43:54 +090038#ifndef CONFIG_MTD_NOR_FLASH
Heiko Schocher45b42912011-05-03 02:15:15 +000039# define OFFSET_ADJUSTMENT 0
40#else
41# define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
42#endif
43
Simon Glass8c6c7c22017-05-17 03:25:38 -060044#ifndef CONFIG_FS_JFFS2
Heiko Schocher07b46272010-01-07 08:55:54 +010045#include <linux/stat.h>
46char *mkmodestr(unsigned long mode, char *str)
47{
48 static const char *l = "xwr";
49 int mask = 1, i;
50 char c;
51
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] = '?';
61 }
62
63 for(i = 0; i < 9; i++) {
64 c = l[i%3];
65 str[9-i] = (mode & mask)?c:'-';
66 mask = mask<<1;
67 }
68
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';
72 str[10] = '\0';
73 return str;
74}
Simon Glass8c6c7c22017-05-17 03:25:38 -060075#endif /* CONFIG_FS_JFFS2 */
Heiko Schocher07b46272010-01-07 08:55:54 +010076
77extern int cramfs_check (struct part_info *info);
78extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
79extern int cramfs_ls (struct part_info *info, char *filename);
80extern int cramfs_info (struct part_info *info);
81
82/***************************************************/
Bin Meng75574052016-02-05 19:30:11 -080083/* U-Boot commands */
Heiko Schocher07b46272010-01-07 08:55:54 +010084/***************************************************/
85
86/**
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.
90 *
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
96 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +020097int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher07b46272010-01-07 08:55:54 +010098{
99 char *filename;
100 int size;
Simon Glass892265d2019-12-28 10:45:02 -0700101 ulong offset = image_load_addr;
Tyler Hall59417d02017-04-12 16:29:15 -0400102 char *offset_virt;
Heiko Schocher07b46272010-01-07 08:55:54 +0100103
104 struct part_info part;
105 struct mtd_device dev;
106 struct mtdids id;
107
108 ulong addr;
Simon Glass64b723f2017-08-03 12:22:12 -0600109 addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
Heiko Schocher07b46272010-01-07 08:55:54 +0100110
111 /* hack! */
112 /* cramfs_* only supports NOR flash chips */
113 /* fake the device type */
114 id.type = MTD_DEV_TYPE_NOR;
115 id.num = 0;
116 dev.id = &id;
117 part.dev = &dev;
118 /* fake the address offset */
Tyler Hall59417d02017-04-12 16:29:15 -0400119 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
Heiko Schocher07b46272010-01-07 08:55:54 +0100120
121 /* pre-set Boot file name */
Simon Glass64b723f2017-08-03 12:22:12 -0600122 filename = env_get("bootfile");
123 if (!filename)
Heiko Schocher07b46272010-01-07 08:55:54 +0100124 filename = "uImage";
Heiko Schocher07b46272010-01-07 08:55:54 +0100125
126 if (argc == 2) {
127 filename = argv[1];
128 }
129 if (argc == 3) {
130 offset = simple_strtoul(argv[1], NULL, 0);
Simon Glass892265d2019-12-28 10:45:02 -0700131 image_load_addr = offset;
Heiko Schocher07b46272010-01-07 08:55:54 +0100132 filename = argv[2];
133 }
134
Tyler Hall59417d02017-04-12 16:29:15 -0400135 offset_virt = map_sysmem(offset, 0);
Heiko Schocher07b46272010-01-07 08:55:54 +0100136 size = 0;
137 if (cramfs_check(&part))
Tyler Hall59417d02017-04-12 16:29:15 -0400138 size = cramfs_load (offset_virt, &part, filename);
Heiko Schocher07b46272010-01-07 08:55:54 +0100139
140 if (size > 0) {
Heiko Schocher07b46272010-01-07 08:55:54 +0100141 printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
142 size, offset);
Simon Glass4d949a22017-08-03 12:22:10 -0600143 env_set_hex("filesize", size);
Heiko Schocher07b46272010-01-07 08:55:54 +0100144 } else {
145 printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
146 }
147
Tyler Hall59417d02017-04-12 16:29:15 -0400148 unmap_sysmem(offset_virt);
149 unmap_sysmem((void *)(uintptr_t)part.offset);
150
Heiko Schocher07b46272010-01-07 08:55:54 +0100151 return !(size > 0);
152}
153
154/**
155 * Routine implementing u-boot ls command which lists content of a given
156 * directory at location 'cramfsaddr'.
157 * cramfsaddr is an evironment variable.
158 *
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
164 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200165int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher07b46272010-01-07 08:55:54 +0100166{
167 char *filename = "/";
168 int ret;
169 struct part_info part;
170 struct mtd_device dev;
171 struct mtdids id;
172
173 ulong addr;
Simon Glass64b723f2017-08-03 12:22:12 -0600174 addr = simple_strtoul(env_get("cramfsaddr"), NULL, 16);
Heiko Schocher07b46272010-01-07 08:55:54 +0100175
176 /* hack! */
177 /* cramfs_* only supports NOR flash chips */
178 /* fake the device type */
179 id.type = MTD_DEV_TYPE_NOR;
180 id.num = 0;
181 dev.id = &id;
182 part.dev = &dev;
183 /* fake the address offset */
Tyler Hall59417d02017-04-12 16:29:15 -0400184 part.offset = (u64)(uintptr_t) map_sysmem(addr - OFFSET_ADJUSTMENT, 0);
Heiko Schocher07b46272010-01-07 08:55:54 +0100185
186 if (argc == 2)
187 filename = argv[1];
188
189 ret = 0;
190 if (cramfs_check(&part))
191 ret = cramfs_ls (&part, filename);
Tyler Hall59417d02017-04-12 16:29:15 -0400192 unmap_sysmem((void *)(uintptr_t)part.offset);
Heiko Schocher07b46272010-01-07 08:55:54 +0100193
194 return ret ? 0 : 1;
195}
196
197/* command line only */
198
199/***************************************************/
200U_BOOT_CMD(
201 cramfsload, 3, 0, do_cramfs_load,
Frans Meulenbroeks0dddc002010-07-31 15:01:52 +0200202 "load binary file from a filesystem image",
Heiko Schocher07b46272010-01-07 08:55:54 +0100203 "[ off ] [ filename ]\n"
204 " - load binary file from address 'cramfsaddr'\n"
205 " with offset 'off'\n"
206);
207U_BOOT_CMD(
208 cramfsls, 2, 1, do_cramfs_ls,
Frans Meulenbroeks0dddc002010-07-31 15:01:52 +0200209 "list files in a directory (default /)",
Heiko Schocher07b46272010-01-07 08:55:54 +0100210 "[ directory ]\n"
211 " - list files in a directory.\n"
212);