blob: 0e43ab67c046781ac1e1b53a0bdd3de544dc8124 [file] [log] [blame]
Heiko Schocher88ffb262010-01-07 08:55:54 +01001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 *
17 * based on: cmd_jffs2.c
18 *
19 * Add support for a CRAMFS located in RAM
20 */
21
22
23/*
24 * CRAMFS support
25 */
26#include <common.h>
27#include <command.h>
28#include <malloc.h>
29#include <linux/list.h>
30#include <linux/ctype.h>
31#include <jffs2/jffs2.h>
32#include <jffs2/load_kernel.h>
33#include <cramfs/cramfs_fs.h>
34
35/* enable/disable debugging messages */
36#define DEBUG_CRAMFS
37#undef DEBUG_CRAMFS
38
39#ifdef DEBUG_CRAMFS
40# define DEBUGF(fmt, args...) printf(fmt ,##args)
41#else
42# define DEBUGF(fmt, args...)
43#endif
44
45#ifdef CONFIG_CRAMFS_CMDLINE
Heiko Schocheree8bc962011-05-03 02:15:01 +000046#include <flash.h>
Heiko Schocher88ffb262010-01-07 08:55:54 +010047
Heiko Schocher62a813b2011-05-03 02:15:15 +000048#ifdef CONFIG_SYS_NO_FLASH
49# define OFFSET_ADJUSTMENT 0
50#else
51# define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
52#endif
53
Heiko Schocher88ffb262010-01-07 08:55:54 +010054#ifndef CONFIG_CMD_JFFS2
55#include <linux/stat.h>
56char *mkmodestr(unsigned long mode, char *str)
57{
58 static const char *l = "xwr";
59 int mask = 1, i;
60 char c;
61
62 switch (mode & S_IFMT) {
63 case S_IFDIR: str[0] = 'd'; break;
64 case S_IFBLK: str[0] = 'b'; break;
65 case S_IFCHR: str[0] = 'c'; break;
66 case S_IFIFO: str[0] = 'f'; break;
67 case S_IFLNK: str[0] = 'l'; break;
68 case S_IFSOCK: str[0] = 's'; break;
69 case S_IFREG: str[0] = '-'; break;
70 default: str[0] = '?';
71 }
72
73 for(i = 0; i < 9; i++) {
74 c = l[i%3];
75 str[9-i] = (mode & mask)?c:'-';
76 mask = mask<<1;
77 }
78
79 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
80 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
81 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
82 str[10] = '\0';
83 return str;
84}
85#endif /* CONFIG_CMD_JFFS2 */
86
87extern int cramfs_check (struct part_info *info);
88extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
89extern int cramfs_ls (struct part_info *info, char *filename);
90extern int cramfs_info (struct part_info *info);
91
92/***************************************************/
93/* U-boot commands */
94/***************************************************/
95
96/**
97 * Routine implementing fsload u-boot command. This routine tries to load
98 * a requested file from cramfs filesystem at location 'cramfsaddr'.
99 * cramfsaddr is an evironment variable.
100 *
101 * @param cmdtp command internal data
102 * @param flag command flag
103 * @param argc number of arguments supplied to the command
104 * @param argv arguments list
105 * @return 0 on success, 1 otherwise
106 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200107int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher88ffb262010-01-07 08:55:54 +0100108{
109 char *filename;
110 int size;
111 ulong offset = load_addr;
112
113 struct part_info part;
114 struct mtd_device dev;
115 struct mtdids id;
116
117 ulong addr;
118 addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
119
120 /* hack! */
121 /* cramfs_* only supports NOR flash chips */
122 /* fake the device type */
123 id.type = MTD_DEV_TYPE_NOR;
124 id.num = 0;
125 dev.id = &id;
126 part.dev = &dev;
127 /* fake the address offset */
Heiko Schocher62a813b2011-05-03 02:15:15 +0000128 part.offset = addr - OFFSET_ADJUSTMENT;
Heiko Schocher88ffb262010-01-07 08:55:54 +0100129
130 /* pre-set Boot file name */
131 if ((filename = getenv("bootfile")) == NULL) {
132 filename = "uImage";
133 }
134
135 if (argc == 2) {
136 filename = argv[1];
137 }
138 if (argc == 3) {
139 offset = simple_strtoul(argv[1], NULL, 0);
140 load_addr = offset;
141 filename = argv[2];
142 }
143
144 size = 0;
145 if (cramfs_check(&part))
146 size = cramfs_load ((char *) offset, &part, filename);
147
148 if (size > 0) {
Heiko Schocher88ffb262010-01-07 08:55:54 +0100149 printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
150 size, offset);
Simon Glass41ef3722013-02-24 17:33:22 +0000151 setenv_hex("filesize", size);
Heiko Schocher88ffb262010-01-07 08:55:54 +0100152 } else {
153 printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
154 }
155
156 return !(size > 0);
157}
158
159/**
160 * Routine implementing u-boot ls command which lists content of a given
161 * directory at location 'cramfsaddr'.
162 * cramfsaddr is an evironment variable.
163 *
164 * @param cmdtp command internal data
165 * @param flag command flag
166 * @param argc number of arguments supplied to the command
167 * @param argv arguments list
168 * @return 0 on success, 1 otherwise
169 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200170int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Heiko Schocher88ffb262010-01-07 08:55:54 +0100171{
172 char *filename = "/";
173 int ret;
174 struct part_info part;
175 struct mtd_device dev;
176 struct mtdids id;
177
178 ulong addr;
179 addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
180
181 /* hack! */
182 /* cramfs_* only supports NOR flash chips */
183 /* fake the device type */
184 id.type = MTD_DEV_TYPE_NOR;
185 id.num = 0;
186 dev.id = &id;
187 part.dev = &dev;
188 /* fake the address offset */
Heiko Schocher62a813b2011-05-03 02:15:15 +0000189 part.offset = addr - OFFSET_ADJUSTMENT;
Heiko Schocher88ffb262010-01-07 08:55:54 +0100190
191 if (argc == 2)
192 filename = argv[1];
193
194 ret = 0;
195 if (cramfs_check(&part))
196 ret = cramfs_ls (&part, filename);
197
198 return ret ? 0 : 1;
199}
200
201/* command line only */
202
203/***************************************************/
204U_BOOT_CMD(
205 cramfsload, 3, 0, do_cramfs_load,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200206 "load binary file from a filesystem image",
Heiko Schocher88ffb262010-01-07 08:55:54 +0100207 "[ off ] [ filename ]\n"
208 " - load binary file from address 'cramfsaddr'\n"
209 " with offset 'off'\n"
210);
211U_BOOT_CMD(
212 cramfsls, 2, 1, do_cramfs_ls,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200213 "list files in a directory (default /)",
Heiko Schocher88ffb262010-01-07 08:55:54 +0100214 "[ directory ]\n"
215 " - list files in a directory.\n"
216);
217
218#endif /* #ifdef CONFIG_CRAMFS_CMDLINE */
219
220/***************************************************/