blob: a3ba91cdab898530fd006cdae85754c2a6888004 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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>
Nye Liu889d51a2008-10-15 22:01:40 -07009#include <linux/utime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11static __initdata char *message;
12static void __init error(char *x)
13{
14 if (!message)
15 message = x;
16}
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/* link hash */
19
Mark Huang6a050da2006-05-15 09:44:03 -070020#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static __initdata struct hash {
23 int ino, minor, major;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070024 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 struct hash *next;
Mark Huang6a050da2006-05-15 09:44:03 -070026 char name[N_ALIGN(PATH_MAX)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070027} *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
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070036static char __init *find_link(int major, int minor, int ino,
37 mode_t mode, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
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;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070047 if (((*p)->mode ^ mode) & S_IFMT)
48 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return (*p)->name;
50 }
Thomas Petazzoni3265e662008-04-29 00:59:43 -070051 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!q)
53 panic("can't allocate link hash entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 q->major = major;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070055 q->minor = minor;
56 q->ino = ino;
57 q->mode = mode;
Mark Huang6a050da2006-05-15 09:44:03 -070058 strcpy(q->name, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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;
Thomas Petazzoni3265e662008-04-29 00:59:43 -070071 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73 }
74}
75
Nye Liu889d51a2008-10-15 22:01:40 -070076static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* 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];
Nye Liu889d51a2008-10-15 22:01:40 -0700144 mtime = parsed[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 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
Al Virob0a5ab92007-07-26 17:33:59 +0100171static inline void __init eat(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 victim += n;
174 this_header += n;
175 count -= n;
176}
177
Nye Liu889d51a2008-10-15 22:01:40 -0700178static __initdata char *vcollected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static __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{
Arjan van de Ven2e591bb2006-12-06 20:37:19 -0800221 if (memcmp(collected, "070707", 6)==0) {
222 error("incorrect cpio method used: use -H newc option");
223 return 1;
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700277 char *old = find_link(major, minor, ino, mode, collected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (old)
279 return (sys_link(old, collected) < 0) ? -1 : 1;
280 }
281 return 0;
282}
283
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700284static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static __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;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700308 clean_path(collected, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (S_ISREG(mode)) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700310 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (wfd >= 0) {
318 sys_fchown(wfd, uid, gid);
319 sys_fchmod(wfd, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700320 vcollected = kstrdup(collected, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 state = CopyFile;
322 }
323 }
324 } else if (S_ISDIR(mode)) {
325 sys_mkdir(collected, mode);
326 sys_chown(collected, uid, gid);
327 sys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700328 dir_add(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
330 S_ISFIFO(mode) || S_ISSOCK(mode)) {
331 if (maybe_link() == 0) {
332 sys_mknod(collected, mode, rdev);
333 sys_chown(collected, uid, gid);
334 sys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700335 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 }
338 return 0;
339}
340
341static int __init do_copy(void)
342{
343 if (count >= body_len) {
344 sys_write(wfd, victim, body_len);
345 sys_close(wfd);
Nye Liu889d51a2008-10-15 22:01:40 -0700346 do_utime(vcollected, mtime);
347 kfree(vcollected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 eat(body_len);
349 state = SkipIt;
350 return 0;
351 } else {
352 sys_write(wfd, victim, count);
353 body_len -= count;
354 eat(count);
355 return 1;
356 }
357}
358
359static int __init do_symlink(void)
360{
361 collected[N_ALIGN(name_len) + body_len] = '\0';
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700362 clean_path(collected, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sys_symlink(collected + N_ALIGN(name_len), collected);
364 sys_lchown(collected, uid, gid);
Nye Liu889d51a2008-10-15 22:01:40 -0700365 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 state = SkipIt;
367 next_state = Reset;
368 return 0;
369}
370
371static __initdata int (*actions[])(void) = {
372 [Start] = do_start,
373 [Collect] = do_collect,
374 [GotHeader] = do_header,
375 [SkipIt] = do_skip,
376 [GotName] = do_name,
377 [CopyFile] = do_copy,
378 [GotSymlink] = do_symlink,
379 [Reset] = do_reset,
380};
381
382static int __init write_buffer(char *buf, unsigned len)
383{
384 count = len;
385 victim = buf;
386
387 while (!actions[state]())
388 ;
389 return len - count;
390}
391
Alain Knaffa26ee602009-01-07 00:10:27 -0800392#if defined CONFIG_RD_GZIP || defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA
Alain Knaff30d65db2009-01-04 22:46:17 +0100393static int __init flush_buffer(void *bufv, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Alain Knaff30d65db2009-01-04 22:46:17 +0100395 char *buf = (char *) bufv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int written;
Alain Knaff30d65db2009-01-04 22:46:17 +0100397 int origLen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (message)
Alain Knaff30d65db2009-01-04 22:46:17 +0100399 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 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 }
Alain Knaff30d65db2009-01-04 22:46:17 +0100413 return origLen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
Alain Knaffa26ee602009-01-07 00:10:27 -0800415#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Alain Knaff30d65db2009-01-04 22:46:17 +0100417static unsigned my_inptr; /* index of next byte to be processed in inbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alain Knaff30d65db2009-01-04 22:46:17 +0100419#include <linux/decompress/bunzip2.h>
420#include <linux/decompress/unlzma.h>
421#include <linux/decompress/inflate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
424{
425 int written;
426 dry_run = check_only;
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700427 header_buf = kmalloc(110, GFP_KERNEL);
428 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
429 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
Alain Knaff30d65db2009-01-04 22:46:17 +0100430
431 if (!header_buf || !symlink_buf || !name_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 panic("can't allocate buffers");
Alain Knaff30d65db2009-01-04 22:46:17 +0100433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 state = Start;
435 this_header = 0;
436 message = NULL;
437 while (!message && len) {
438 loff_t saved_offset = this_header;
439 if (*buf == '0' && !(this_header & 3)) {
440 state = Start;
441 written = write_buffer(buf, len);
442 buf += written;
443 len -= written;
444 continue;
445 }
446 if (!*buf) {
447 buf++;
448 len--;
449 this_header++;
450 continue;
451 }
452 this_header = 0;
Alain Knaffa26ee602009-01-07 00:10:27 -0800453#ifdef CONFIG_RD_GZIP
Alain Knaff30d65db2009-01-04 22:46:17 +0100454 if (!gunzip(buf, len, NULL, flush_buffer, NULL,
455 &my_inptr, error) &&
456 message == NULL)
457 goto ok;
Alain Knaffa26ee602009-01-07 00:10:27 -0800458#endif
Alain Knaff30d65db2009-01-04 22:46:17 +0100459
460#ifdef CONFIG_RD_BZIP2
461 message = NULL; /* Zero out message, or else cpio will
462 think an error has already occured */
463 if (!bunzip2(buf, len, NULL, flush_buffer, NULL,
464 &my_inptr, error) &&
465 message == NULL) {
466 goto ok;
467 }
468#endif
469
470#ifdef CONFIG_RD_LZMA
471 message = NULL; /* Zero out message, or else cpio will
472 think an error has already occured */
473 if (!unlzma(buf, len, NULL, flush_buffer, NULL,
474 &my_inptr, error) &&
475 message == NULL) {
476 goto ok;
477 }
478#endif
Alain Knaffa26ee602009-01-07 00:10:27 -0800479#if defined CONFIG_RD_GZIP || defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA
Alain Knaff30d65db2009-01-04 22:46:17 +0100480ok:
Alain Knaffa26ee602009-01-07 00:10:27 -0800481#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (state != Reset)
Alain Knaff30d65db2009-01-04 22:46:17 +0100483 error("junk in compressed archive");
484 this_header = saved_offset + my_inptr;
485 buf += my_inptr;
486 len -= my_inptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Nye Liu889d51a2008-10-15 22:01:40 -0700488 dir_utime();
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700489 kfree(name_buf);
490 kfree(symlink_buf);
491 kfree(header_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return message;
493}
494
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800495static int __initdata do_retain_initrd;
496
497static int __init retain_initrd_param(char *str)
498{
499 if (*str)
500 return 0;
501 do_retain_initrd = 1;
502 return 1;
503}
504__setup("retain_initrd", retain_initrd_param);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506extern char __initramfs_start[], __initramfs_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507#include <linux/initrd.h>
Haren Myneni9c15e852006-02-10 01:51:05 -0800508#include <linux/kexec.h>
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700509
510static void __init free_initrd(void)
511{
Haren Myneni9c15e852006-02-10 01:51:05 -0800512#ifdef CONFIG_KEXEC
513 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
514 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800515#endif
516 if (do_retain_initrd)
517 goto skip;
Haren Myneni9c15e852006-02-10 01:51:05 -0800518
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800519#ifdef CONFIG_KEXEC
Haren Myneni9c15e852006-02-10 01:51:05 -0800520 /*
521 * If the initrd region is overlapped with crashkernel reserved region,
522 * free only memory that is not part of crashkernel region.
523 */
524 if (initrd_start < crashk_end && initrd_end > crashk_start) {
525 /*
526 * Initialize initrd memory region since the kexec boot does
527 * not do.
528 */
529 memset((void *)initrd_start, 0, initrd_end - initrd_start);
530 if (initrd_start < crashk_start)
531 free_initrd_mem(initrd_start, crashk_start);
532 if (initrd_end > crashk_end)
533 free_initrd_mem(crashk_end, initrd_end);
534 } else
535#endif
536 free_initrd_mem(initrd_start, initrd_end);
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800537skip:
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700538 initrd_start = 0;
539 initrd_end = 0;
540}
541
Linus Torvalds9a9e0d62008-03-15 11:53:32 -0700542static int __init populate_rootfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 char *err = unpack_to_rootfs(__initramfs_start,
545 __initramfs_end - __initramfs_start, 0);
546 if (err)
547 panic(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (initrd_start) {
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800549#ifdef CONFIG_BLK_DEV_RAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int fd;
551 printk(KERN_INFO "checking if image is initramfs...");
552 err = unpack_to_rootfs((char *)initrd_start,
553 initrd_end - initrd_start, 1);
554 if (!err) {
555 printk(" it is\n");
556 unpack_to_rootfs((char *)initrd_start,
557 initrd_end - initrd_start, 0);
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700558 free_initrd();
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 printk("it isn't (%s); looks like an initrd\n", err);
Jason Gunthorpe33644c52006-03-26 01:37:38 -0800562 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (fd >= 0) {
564 sys_write(fd, (char *)initrd_start,
565 initrd_end - initrd_start);
566 sys_close(fd);
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700567 free_initrd();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800569#else
570 printk(KERN_INFO "Unpacking initramfs...");
571 err = unpack_to_rootfs((char *)initrd_start,
572 initrd_end - initrd_start, 0);
573 if (err)
574 panic(err);
575 printk(" done\n");
576 free_initrd();
577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800581rootfs_initcall(populate_rootfs);