]> bbs.cooldavid.org Git - net-next-2.6.git/blame - init/initramfs.c
bzip2/lzma: comprehensible error messages for missing decompressor
[net-next-2.6.git] / init / initramfs.c
CommitLineData
1da177e4
LT
1#include <linux/init.h>
2#include <linux/fs.h>
3#include <linux/slab.h>
4#include <linux/types.h>
5#include <linux/fcntl.h>
6#include <linux/delay.h>
7#include <linux/string.h>
8#include <linux/syscalls.h>
889d51a1 9#include <linux/utime.h>
1da177e4
LT
10
11static __initdata char *message;
12static void __init error(char *x)
13{
14 if (!message)
15 message = x;
16}
17
1da177e4
LT
18/* link hash */
19
6a050da4
MH
20#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
21
1da177e4
LT
22static __initdata struct hash {
23 int ino, minor, major;
2139a7fb 24 mode_t mode;
1da177e4 25 struct hash *next;
6a050da4 26 char name[N_ALIGN(PATH_MAX)];
1da177e4
LT
27} *head[32];
28
29static inline int hash(int major, int minor, int ino)
30{
31 unsigned long tmp = ino + minor + (major << 3);
32 tmp += tmp >> 5;
33 return tmp & 31;
34}
35
2139a7fb
PA
36static char __init *find_link(int major, int minor, int ino,
37 mode_t mode, char *name)
1da177e4
LT
38{
39 struct hash **p, *q;
40 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
41 if ((*p)->ino != ino)
42 continue;
43 if ((*p)->minor != minor)
44 continue;
45 if ((*p)->major != major)
46 continue;
2139a7fb
PA
47 if (((*p)->mode ^ mode) & S_IFMT)
48 continue;
1da177e4
LT
49 return (*p)->name;
50 }
3265e66b 51 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
1da177e4
LT
52 if (!q)
53 panic("can't allocate link hash entry");
1da177e4 54 q->major = major;
2139a7fb
PA
55 q->minor = minor;
56 q->ino = ino;
57 q->mode = mode;
6a050da4 58 strcpy(q->name, name);
1da177e4
LT
59 q->next = NULL;
60 *p = q;
61 return NULL;
62}
63
64static void __init free_hash(void)
65{
66 struct hash **p, *q;
67 for (p = head; p < head + 32; p++) {
68 while (*p) {
69 q = *p;
70 *p = q->next;
3265e66b 71 kfree(q);
1da177e4
LT
72 }
73 }
74}
75
889d51a1
NL
76static long __init do_utime(char __user *filename, time_t mtime)
77{
78 struct timespec t[2];
79
80 t[0].tv_sec = mtime;
81 t[0].tv_nsec = 0;
82 t[1].tv_sec = mtime;
83 t[1].tv_nsec = 0;
84
85 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
86}
87
88static __initdata LIST_HEAD(dir_list);
89struct dir_entry {
90 struct list_head list;
91 char *name;
92 time_t mtime;
93};
94
95static void __init dir_add(const char *name, time_t mtime)
96{
97 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
98 if (!de)
99 panic("can't allocate dir_entry buffer");
100 INIT_LIST_HEAD(&de->list);
101 de->name = kstrdup(name, GFP_KERNEL);
102 de->mtime = mtime;
103 list_add(&de->list, &dir_list);
104}
105
106static void __init dir_utime(void)
107{
108 struct dir_entry *de, *tmp;
109 list_for_each_entry_safe(de, tmp, &dir_list, list) {
110 list_del(&de->list);
111 do_utime(de->name, de->mtime);
112 kfree(de->name);
113 kfree(de);
114 }
115}
116
117static __initdata time_t mtime;
118
1da177e4
LT
119/* cpio header parsing */
120
121static __initdata unsigned long ino, major, minor, nlink;
122static __initdata mode_t mode;
123static __initdata unsigned long body_len, name_len;
124static __initdata uid_t uid;
125static __initdata gid_t gid;
126static __initdata unsigned rdev;
127
128static void __init parse_header(char *s)
129{
130 unsigned long parsed[12];
131 char buf[9];
132 int i;
133
134 buf[8] = '\0';
135 for (i = 0, s += 6; i < 12; i++, s += 8) {
136 memcpy(buf, s, 8);
137 parsed[i] = simple_strtoul(buf, NULL, 16);
138 }
139 ino = parsed[0];
140 mode = parsed[1];
141 uid = parsed[2];
142 gid = parsed[3];
143 nlink = parsed[4];
889d51a1 144 mtime = parsed[5];
1da177e4
LT
145 body_len = parsed[6];
146 major = parsed[7];
147 minor = parsed[8];
148 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
149 name_len = parsed[11];
150}
151
152/* FSM */
153
154static __initdata enum state {
155 Start,
156 Collect,
157 GotHeader,
158 SkipIt,
159 GotName,
160 CopyFile,
161 GotSymlink,
162 Reset
163} state, next_state;
164
165static __initdata char *victim;
166static __initdata unsigned count;
167static __initdata loff_t this_header, next_header;
168
169static __initdata int dry_run;
170
b0a5ab93 171static inline void __init eat(unsigned n)
1da177e4
LT
172{
173 victim += n;
174 this_header += n;
175 count -= n;
176}
177
889d51a1 178static __initdata char *vcollected;
1da177e4
LT
179static __initdata char *collected;
180static __initdata int remains;
181static __initdata char *collect;
182
183static void __init read_into(char *buf, unsigned size, enum state next)
184{
185 if (count >= size) {
186 collected = victim;
187 eat(size);
188 state = next;
189 } else {
190 collect = collected = buf;
191 remains = size;
192 next_state = next;
193 state = Collect;
194 }
195}
196
197static __initdata char *header_buf, *symlink_buf, *name_buf;
198
199static int __init do_start(void)
200{
201 read_into(header_buf, 110, GotHeader);
202 return 0;
203}
204
205static int __init do_collect(void)
206{
207 unsigned n = remains;
208 if (count < n)
209 n = count;
210 memcpy(collect, victim, n);
211 eat(n);
212 collect += n;
213 if ((remains -= n) != 0)
214 return 1;
215 state = next_state;
216 return 0;
217}
218
219static int __init do_header(void)
220{
2e591bbc
AV
221 if (memcmp(collected, "070707", 6)==0) {
222 error("incorrect cpio method used: use -H newc option");
223 return 1;
224 }
1da177e4
LT
225 if (memcmp(collected, "070701", 6)) {
226 error("no cpio magic");
227 return 1;
228 }
229 parse_header(collected);
230 next_header = this_header + N_ALIGN(name_len) + body_len;
231 next_header = (next_header + 3) & ~3;
232 if (dry_run) {
233 read_into(name_buf, N_ALIGN(name_len), GotName);
234 return 0;
235 }
236 state = SkipIt;
237 if (name_len <= 0 || name_len > PATH_MAX)
238 return 0;
239 if (S_ISLNK(mode)) {
240 if (body_len > PATH_MAX)
241 return 0;
242 collect = collected = symlink_buf;
243 remains = N_ALIGN(name_len) + body_len;
244 next_state = GotSymlink;
245 state = Collect;
246 return 0;
247 }
248 if (S_ISREG(mode) || !body_len)
249 read_into(name_buf, N_ALIGN(name_len), GotName);
250 return 0;
251}
252
253static int __init do_skip(void)
254{
255 if (this_header + count < next_header) {
256 eat(count);
257 return 1;
258 } else {
259 eat(next_header - this_header);
260 state = next_state;
261 return 0;
262 }
263}
264
265static int __init do_reset(void)
266{
267 while(count && *victim == '\0')
268 eat(1);
269 if (count && (this_header & 3))
270 error("broken padding");
271 return 1;
272}
273
274static int __init maybe_link(void)
275{
276 if (nlink >= 2) {
2139a7fb 277 char *old = find_link(major, minor, ino, mode, collected);
1da177e4
LT
278 if (old)
279 return (sys_link(old, collected) < 0) ? -1 : 1;
280 }
281 return 0;
282}
283
2139a7fb
PA
284static void __init clean_path(char *path, mode_t mode)
285{
286 struct stat st;
287
288 if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
289 if (S_ISDIR(st.st_mode))
290 sys_rmdir(path);
291 else
292 sys_unlink(path);
293 }
294}
295
1da177e4
LT
296static __initdata int wfd;
297
298static int __init do_name(void)
299{
300 state = SkipIt;
301 next_state = Reset;
302 if (strcmp(collected, "TRAILER!!!") == 0) {
303 free_hash();
304 return 0;
305 }
306 if (dry_run)
307 return 0;
2139a7fb 308 clean_path(collected, mode);
1da177e4 309 if (S_ISREG(mode)) {
2139a7fb
PA
310 int ml = maybe_link();
311 if (ml >= 0) {
312 int openflags = O_WRONLY|O_CREAT;
313 if (ml != 1)
314 openflags |= O_TRUNC;
315 wfd = sys_open(collected, openflags, mode);
316
1da177e4
LT
317 if (wfd >= 0) {
318 sys_fchown(wfd, uid, gid);
319 sys_fchmod(wfd, mode);
cb6ff208 320 sys_ftruncate(wfd, body_len);
889d51a1 321 vcollected = kstrdup(collected, GFP_KERNEL);
1da177e4
LT
322 state = CopyFile;
323 }
324 }
325 } else if (S_ISDIR(mode)) {
326 sys_mkdir(collected, mode);
327 sys_chown(collected, uid, gid);
328 sys_chmod(collected, mode);
889d51a1 329 dir_add(collected, mtime);
1da177e4
LT
330 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
331 S_ISFIFO(mode) || S_ISSOCK(mode)) {
332 if (maybe_link() == 0) {
333 sys_mknod(collected, mode, rdev);
334 sys_chown(collected, uid, gid);
335 sys_chmod(collected, mode);
889d51a1 336 do_utime(collected, mtime);
1da177e4
LT
337 }
338 }
339 return 0;
340}
341
342static int __init do_copy(void)
343{
344 if (count >= body_len) {
345 sys_write(wfd, victim, body_len);
346 sys_close(wfd);
889d51a1
NL
347 do_utime(vcollected, mtime);
348 kfree(vcollected);
1da177e4
LT
349 eat(body_len);
350 state = SkipIt;
351 return 0;
352 } else {
353 sys_write(wfd, victim, count);
354 body_len -= count;
355 eat(count);
356 return 1;
357 }
358}
359
360static int __init do_symlink(void)
361{
362 collected[N_ALIGN(name_len) + body_len] = '\0';
2139a7fb 363 clean_path(collected, 0);
1da177e4
LT
364 sys_symlink(collected + N_ALIGN(name_len), collected);
365 sys_lchown(collected, uid, gid);
889d51a1 366 do_utime(collected, mtime);
1da177e4
LT
367 state = SkipIt;
368 next_state = Reset;
369 return 0;
370}
371
372static __initdata int (*actions[])(void) = {
373 [Start] = do_start,
374 [Collect] = do_collect,
375 [GotHeader] = do_header,
376 [SkipIt] = do_skip,
377 [GotName] = do_name,
378 [CopyFile] = do_copy,
379 [GotSymlink] = do_symlink,
380 [Reset] = do_reset,
381};
382
383static int __init write_buffer(char *buf, unsigned len)
384{
385 count = len;
386 victim = buf;
387
388 while (!actions[state]())
389 ;
390 return len - count;
391}
392
30d65dbf 393static int __init flush_buffer(void *bufv, unsigned len)
1da177e4 394{
30d65dbf 395 char *buf = (char *) bufv;
1da177e4 396 int written;
30d65dbf 397 int origLen = len;
1da177e4 398 if (message)
30d65dbf 399 return -1;
1da177e4
LT
400 while ((written = write_buffer(buf, len)) < len && !message) {
401 char c = buf[written];
402 if (c == '0') {
403 buf += written;
404 len -= written;
405 state = Start;
406 } else if (c == 0) {
407 buf += written;
408 len -= written;
409 state = Reset;
410 } else
411 error("junk in compressed archive");
412 }
30d65dbf 413 return origLen;
1da177e4
LT
414}
415
30d65dbf 416static unsigned my_inptr; /* index of next byte to be processed in inbuf */
1da177e4 417
889c92d2 418#include <linux/decompress/generic.h>
1da177e4
LT
419
420static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
421{
422 int written;
889c92d2 423 decompress_fn decompress;
23a22d57
PA
424 const char *compress_name;
425 static __initdata char msg_buf[64];
889c92d2 426
1da177e4 427 dry_run = check_only;
3265e66b
TP
428 header_buf = kmalloc(110, GFP_KERNEL);
429 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
430 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
30d65dbf
AK
431
432 if (!header_buf || !symlink_buf || !name_buf)
1da177e4 433 panic("can't allocate buffers");
30d65dbf 434
1da177e4
LT
435 state = Start;
436 this_header = 0;
437 message = NULL;
438 while (!message && len) {
439 loff_t saved_offset = this_header;
440 if (*buf == '0' && !(this_header & 3)) {
441 state = Start;
442 written = write_buffer(buf, len);
443 buf += written;
444 len -= written;
445 continue;
446 }
447 if (!*buf) {
448 buf++;
449 len--;
450 this_header++;
451 continue;
452 }
453 this_header = 0;
23a22d57 454 decompress = decompress_method(buf, len, &compress_name);
889c92d2
PA
455 if (decompress)
456 decompress(buf, len, NULL, flush_buffer, NULL,
457 &my_inptr, error);
23a22d57
PA
458 else if (compress_name) {
459 if (!message) {
460 snprintf(msg_buf, sizeof msg_buf,
461 "compression method %s not configured",
462 compress_name);
463 message = msg_buf;
464 }
465 }
1da177e4 466 if (state != Reset)
30d65dbf
AK
467 error("junk in compressed archive");
468 this_header = saved_offset + my_inptr;
469 buf += my_inptr;
470 len -= my_inptr;
1da177e4 471 }
889d51a1 472 dir_utime();
3265e66b
TP
473 kfree(name_buf);
474 kfree(symlink_buf);
475 kfree(header_buf);
1da177e4
LT
476 return message;
477}
478
0a7b35cb
MN
479static int __initdata do_retain_initrd;
480
481static int __init retain_initrd_param(char *str)
482{
483 if (*str)
484 return 0;
485 do_retain_initrd = 1;
486 return 1;
487}
488__setup("retain_initrd", retain_initrd_param);
489
1da177e4 490extern char __initramfs_start[], __initramfs_end[];
1da177e4 491#include <linux/initrd.h>
9c15e852 492#include <linux/kexec.h>
0f3d2bd5
JB
493
494static void __init free_initrd(void)
495{
9c15e852
HM
496#ifdef CONFIG_KEXEC
497 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
498 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
0a7b35cb
MN
499#endif
500 if (do_retain_initrd)
501 goto skip;
9c15e852 502
0a7b35cb 503#ifdef CONFIG_KEXEC
9c15e852
HM
504 /*
505 * If the initrd region is overlapped with crashkernel reserved region,
506 * free only memory that is not part of crashkernel region.
507 */
508 if (initrd_start < crashk_end && initrd_end > crashk_start) {
509 /*
510 * Initialize initrd memory region since the kexec boot does
511 * not do.
512 */
513 memset((void *)initrd_start, 0, initrd_end - initrd_start);
514 if (initrd_start < crashk_start)
515 free_initrd_mem(initrd_start, crashk_start);
516 if (initrd_end > crashk_end)
517 free_initrd_mem(crashk_end, initrd_end);
518 } else
519#endif
520 free_initrd_mem(initrd_start, initrd_end);
0a7b35cb 521skip:
0f3d2bd5
JB
522 initrd_start = 0;
523 initrd_end = 0;
524}
525
9a9e0d68 526static int __init populate_rootfs(void)
1da177e4
LT
527{
528 char *err = unpack_to_rootfs(__initramfs_start,
529 __initramfs_end - __initramfs_start, 0);
530 if (err)
531 panic(err);
1da177e4 532 if (initrd_start) {
340e48e6 533#ifdef CONFIG_BLK_DEV_RAM
1da177e4
LT
534 int fd;
535 printk(KERN_INFO "checking if image is initramfs...");
536 err = unpack_to_rootfs((char *)initrd_start,
537 initrd_end - initrd_start, 1);
538 if (!err) {
539 printk(" it is\n");
540 unpack_to_rootfs((char *)initrd_start,
541 initrd_end - initrd_start, 0);
0f3d2bd5 542 free_initrd();
8d610dd5 543 return 0;
1da177e4
LT
544 }
545 printk("it isn't (%s); looks like an initrd\n", err);
33644c5e 546 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
1da177e4
LT
547 if (fd >= 0) {
548 sys_write(fd, (char *)initrd_start,
549 initrd_end - initrd_start);
550 sys_close(fd);
0f3d2bd5 551 free_initrd();
1da177e4 552 }
340e48e6
ZP
553#else
554 printk(KERN_INFO "Unpacking initramfs...");
555 err = unpack_to_rootfs((char *)initrd_start,
556 initrd_end - initrd_start, 0);
557 if (err)
558 panic(err);
559 printk(" done\n");
560 free_initrd();
561#endif
1da177e4 562 }
8d610dd5 563 return 0;
1da177e4 564}
8d610dd5 565rootfs_initcall(populate_rootfs);