]>
Commit | Line | Data |
---|---|---|
c67e5382 HS |
1 | /* |
2 | * Many of the syscalls used in this file expect some of the arguments | |
3 | * to be __user pointers not __kernel pointers. To limit the sparse | |
4 | * noise, turn off sparse checking for this file. | |
5 | */ | |
6 | #ifdef __CHECKER__ | |
7 | #undef __CHECKER__ | |
8 | #warning "Sparse checking disabled for this file" | |
9 | #endif | |
10 | ||
1da177e4 LT |
11 | #include <linux/init.h> |
12 | #include <linux/fs.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/fcntl.h> | |
16 | #include <linux/delay.h> | |
17 | #include <linux/string.h> | |
df52092f | 18 | #include <linux/dirent.h> |
1da177e4 | 19 | #include <linux/syscalls.h> |
889d51a1 | 20 | #include <linux/utime.h> |
08865514 | 21 | #include <linux/file.h> |
1da177e4 | 22 | |
38747439 YL |
23 | static ssize_t __init xwrite(int fd, const char *p, size_t count) |
24 | { | |
25 | ssize_t out = 0; | |
26 | ||
27 | /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ | |
28 | while (count) { | |
29 | ssize_t rv = sys_write(fd, p, count); | |
30 | ||
31 | if (rv < 0) { | |
32 | if (rv == -EINTR || rv == -EAGAIN) | |
33 | continue; | |
34 | return out ? out : rv; | |
35 | } else if (rv == 0) | |
36 | break; | |
37 | ||
38 | p += rv; | |
39 | out += rv; | |
40 | count -= rv; | |
41 | } | |
42 | ||
43 | return out; | |
44 | } | |
45 | ||
1da177e4 LT |
46 | static __initdata char *message; |
47 | static void __init error(char *x) | |
48 | { | |
49 | if (!message) | |
50 | message = x; | |
51 | } | |
52 | ||
1da177e4 LT |
53 | /* link hash */ |
54 | ||
6a050da4 MH |
55 | #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) |
56 | ||
1da177e4 LT |
57 | static __initdata struct hash { |
58 | int ino, minor, major; | |
685dd2d5 | 59 | umode_t mode; |
1da177e4 | 60 | struct hash *next; |
6a050da4 | 61 | char name[N_ALIGN(PATH_MAX)]; |
1da177e4 LT |
62 | } *head[32]; |
63 | ||
64 | static inline int hash(int major, int minor, int ino) | |
65 | { | |
66 | unsigned long tmp = ino + minor + (major << 3); | |
67 | tmp += tmp >> 5; | |
68 | return tmp & 31; | |
69 | } | |
70 | ||
2139a7fb | 71 | static char __init *find_link(int major, int minor, int ino, |
685dd2d5 | 72 | umode_t mode, char *name) |
1da177e4 LT |
73 | { |
74 | struct hash **p, *q; | |
75 | for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { | |
76 | if ((*p)->ino != ino) | |
77 | continue; | |
78 | if ((*p)->minor != minor) | |
79 | continue; | |
80 | if ((*p)->major != major) | |
81 | continue; | |
2139a7fb PA |
82 | if (((*p)->mode ^ mode) & S_IFMT) |
83 | continue; | |
1da177e4 LT |
84 | return (*p)->name; |
85 | } | |
3265e66b | 86 | q = kmalloc(sizeof(struct hash), GFP_KERNEL); |
1da177e4 LT |
87 | if (!q) |
88 | panic("can't allocate link hash entry"); | |
1da177e4 | 89 | q->major = major; |
2139a7fb PA |
90 | q->minor = minor; |
91 | q->ino = ino; | |
92 | q->mode = mode; | |
6a050da4 | 93 | strcpy(q->name, name); |
1da177e4 LT |
94 | q->next = NULL; |
95 | *p = q; | |
96 | return NULL; | |
97 | } | |
98 | ||
99 | static void __init free_hash(void) | |
100 | { | |
101 | struct hash **p, *q; | |
102 | for (p = head; p < head + 32; p++) { | |
103 | while (*p) { | |
104 | q = *p; | |
105 | *p = q->next; | |
3265e66b | 106 | kfree(q); |
1da177e4 LT |
107 | } |
108 | } | |
109 | } | |
110 | ||
c67e5382 | 111 | static long __init do_utime(char *filename, time_t mtime) |
889d51a1 NL |
112 | { |
113 | struct timespec t[2]; | |
114 | ||
115 | t[0].tv_sec = mtime; | |
116 | t[0].tv_nsec = 0; | |
117 | t[1].tv_sec = mtime; | |
118 | t[1].tv_nsec = 0; | |
119 | ||
120 | return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW); | |
121 | } | |
122 | ||
123 | static __initdata LIST_HEAD(dir_list); | |
124 | struct dir_entry { | |
125 | struct list_head list; | |
126 | char *name; | |
127 | time_t mtime; | |
128 | }; | |
129 | ||
130 | static void __init dir_add(const char *name, time_t mtime) | |
131 | { | |
132 | struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL); | |
133 | if (!de) | |
134 | panic("can't allocate dir_entry buffer"); | |
135 | INIT_LIST_HEAD(&de->list); | |
136 | de->name = kstrdup(name, GFP_KERNEL); | |
137 | de->mtime = mtime; | |
138 | list_add(&de->list, &dir_list); | |
139 | } | |
140 | ||
141 | static void __init dir_utime(void) | |
142 | { | |
143 | struct dir_entry *de, *tmp; | |
144 | list_for_each_entry_safe(de, tmp, &dir_list, list) { | |
145 | list_del(&de->list); | |
146 | do_utime(de->name, de->mtime); | |
147 | kfree(de->name); | |
148 | kfree(de); | |
149 | } | |
150 | } | |
151 | ||
152 | static __initdata time_t mtime; | |
153 | ||
1da177e4 LT |
154 | /* cpio header parsing */ |
155 | ||
156 | static __initdata unsigned long ino, major, minor, nlink; | |
685dd2d5 | 157 | static __initdata umode_t mode; |
1da177e4 LT |
158 | static __initdata unsigned long body_len, name_len; |
159 | static __initdata uid_t uid; | |
160 | static __initdata gid_t gid; | |
161 | static __initdata unsigned rdev; | |
162 | ||
163 | static void __init parse_header(char *s) | |
164 | { | |
165 | unsigned long parsed[12]; | |
166 | char buf[9]; | |
167 | int i; | |
168 | ||
169 | buf[8] = '\0'; | |
170 | for (i = 0, s += 6; i < 12; i++, s += 8) { | |
171 | memcpy(buf, s, 8); | |
172 | parsed[i] = simple_strtoul(buf, NULL, 16); | |
173 | } | |
174 | ino = parsed[0]; | |
175 | mode = parsed[1]; | |
176 | uid = parsed[2]; | |
177 | gid = parsed[3]; | |
178 | nlink = parsed[4]; | |
889d51a1 | 179 | mtime = parsed[5]; |
1da177e4 LT |
180 | body_len = parsed[6]; |
181 | major = parsed[7]; | |
182 | minor = parsed[8]; | |
183 | rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); | |
184 | name_len = parsed[11]; | |
185 | } | |
186 | ||
187 | /* FSM */ | |
188 | ||
189 | static __initdata enum state { | |
190 | Start, | |
191 | Collect, | |
192 | GotHeader, | |
193 | SkipIt, | |
194 | GotName, | |
195 | CopyFile, | |
196 | GotSymlink, | |
197 | Reset | |
198 | } state, next_state; | |
199 | ||
200 | static __initdata char *victim; | |
c34d85ac | 201 | static unsigned long byte_count __initdata; |
1da177e4 LT |
202 | static __initdata loff_t this_header, next_header; |
203 | ||
b0a5ab93 | 204 | static inline void __init eat(unsigned n) |
1da177e4 LT |
205 | { |
206 | victim += n; | |
207 | this_header += n; | |
c34d85ac | 208 | byte_count -= n; |
1da177e4 LT |
209 | } |
210 | ||
889d51a1 | 211 | static __initdata char *vcollected; |
1da177e4 | 212 | static __initdata char *collected; |
d97b07c5 | 213 | static long remains __initdata; |
1da177e4 LT |
214 | static __initdata char *collect; |
215 | ||
216 | static void __init read_into(char *buf, unsigned size, enum state next) | |
217 | { | |
c34d85ac | 218 | if (byte_count >= size) { |
1da177e4 LT |
219 | collected = victim; |
220 | eat(size); | |
221 | state = next; | |
222 | } else { | |
223 | collect = collected = buf; | |
224 | remains = size; | |
225 | next_state = next; | |
226 | state = Collect; | |
227 | } | |
228 | } | |
229 | ||
230 | static __initdata char *header_buf, *symlink_buf, *name_buf; | |
231 | ||
232 | static int __init do_start(void) | |
233 | { | |
234 | read_into(header_buf, 110, GotHeader); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | static int __init do_collect(void) | |
239 | { | |
d97b07c5 | 240 | unsigned long n = remains; |
c34d85ac MR |
241 | if (byte_count < n) |
242 | n = byte_count; | |
1da177e4 LT |
243 | memcpy(collect, victim, n); |
244 | eat(n); | |
245 | collect += n; | |
246 | if ((remains -= n) != 0) | |
247 | return 1; | |
248 | state = next_state; | |
249 | return 0; | |
250 | } | |
251 | ||
252 | static int __init do_header(void) | |
253 | { | |
2e591bbc AV |
254 | if (memcmp(collected, "070707", 6)==0) { |
255 | error("incorrect cpio method used: use -H newc option"); | |
256 | return 1; | |
257 | } | |
1da177e4 LT |
258 | if (memcmp(collected, "070701", 6)) { |
259 | error("no cpio magic"); | |
260 | return 1; | |
261 | } | |
262 | parse_header(collected); | |
263 | next_header = this_header + N_ALIGN(name_len) + body_len; | |
264 | next_header = (next_header + 3) & ~3; | |
1da177e4 LT |
265 | state = SkipIt; |
266 | if (name_len <= 0 || name_len > PATH_MAX) | |
267 | return 0; | |
268 | if (S_ISLNK(mode)) { | |
269 | if (body_len > PATH_MAX) | |
270 | return 0; | |
271 | collect = collected = symlink_buf; | |
272 | remains = N_ALIGN(name_len) + body_len; | |
273 | next_state = GotSymlink; | |
274 | state = Collect; | |
275 | return 0; | |
276 | } | |
277 | if (S_ISREG(mode) || !body_len) | |
278 | read_into(name_buf, N_ALIGN(name_len), GotName); | |
279 | return 0; | |
280 | } | |
281 | ||
282 | static int __init do_skip(void) | |
283 | { | |
c34d85ac MR |
284 | if (this_header + byte_count < next_header) { |
285 | eat(byte_count); | |
1da177e4 LT |
286 | return 1; |
287 | } else { | |
288 | eat(next_header - this_header); | |
289 | state = next_state; | |
290 | return 0; | |
291 | } | |
292 | } | |
293 | ||
294 | static int __init do_reset(void) | |
295 | { | |
c34d85ac | 296 | while (byte_count && *victim == '\0') |
1da177e4 | 297 | eat(1); |
c34d85ac | 298 | if (byte_count && (this_header & 3)) |
1da177e4 LT |
299 | error("broken padding"); |
300 | return 1; | |
301 | } | |
302 | ||
303 | static int __init maybe_link(void) | |
304 | { | |
305 | if (nlink >= 2) { | |
2139a7fb | 306 | char *old = find_link(major, minor, ino, mode, collected); |
1da177e4 LT |
307 | if (old) |
308 | return (sys_link(old, collected) < 0) ? -1 : 1; | |
309 | } | |
310 | return 0; | |
311 | } | |
312 | ||
c34d85ac | 313 | static void __init clean_path(char *path, umode_t fmode) |
2139a7fb PA |
314 | { |
315 | struct stat st; | |
316 | ||
c34d85ac | 317 | if (!sys_newlstat(path, &st) && (st.st_mode ^ fmode) & S_IFMT) { |
2139a7fb PA |
318 | if (S_ISDIR(st.st_mode)) |
319 | sys_rmdir(path); | |
320 | else | |
321 | sys_unlink(path); | |
322 | } | |
323 | } | |
324 | ||
1da177e4 LT |
325 | static __initdata int wfd; |
326 | ||
327 | static int __init do_name(void) | |
328 | { | |
329 | state = SkipIt; | |
330 | next_state = Reset; | |
331 | if (strcmp(collected, "TRAILER!!!") == 0) { | |
332 | free_hash(); | |
333 | return 0; | |
334 | } | |
2139a7fb | 335 | clean_path(collected, mode); |
1da177e4 | 336 | if (S_ISREG(mode)) { |
2139a7fb PA |
337 | int ml = maybe_link(); |
338 | if (ml >= 0) { | |
339 | int openflags = O_WRONLY|O_CREAT; | |
340 | if (ml != 1) | |
341 | openflags |= O_TRUNC; | |
342 | wfd = sys_open(collected, openflags, mode); | |
343 | ||
1da177e4 LT |
344 | if (wfd >= 0) { |
345 | sys_fchown(wfd, uid, gid); | |
346 | sys_fchmod(wfd, mode); | |
d20d5a74 RR |
347 | if (body_len) |
348 | sys_ftruncate(wfd, body_len); | |
889d51a1 | 349 | vcollected = kstrdup(collected, GFP_KERNEL); |
1da177e4 LT |
350 | state = CopyFile; |
351 | } | |
352 | } | |
353 | } else if (S_ISDIR(mode)) { | |
354 | sys_mkdir(collected, mode); | |
355 | sys_chown(collected, uid, gid); | |
356 | sys_chmod(collected, mode); | |
889d51a1 | 357 | dir_add(collected, mtime); |
1da177e4 LT |
358 | } else if (S_ISBLK(mode) || S_ISCHR(mode) || |
359 | S_ISFIFO(mode) || S_ISSOCK(mode)) { | |
360 | if (maybe_link() == 0) { | |
361 | sys_mknod(collected, mode, rdev); | |
362 | sys_chown(collected, uid, gid); | |
363 | sys_chmod(collected, mode); | |
889d51a1 | 364 | do_utime(collected, mtime); |
1da177e4 LT |
365 | } |
366 | } | |
367 | return 0; | |
368 | } | |
369 | ||
370 | static int __init do_copy(void) | |
371 | { | |
c34d85ac | 372 | if (byte_count >= body_len) { |
9687fd91 DE |
373 | if (xwrite(wfd, victim, body_len) != body_len) |
374 | error("write error"); | |
1da177e4 | 375 | sys_close(wfd); |
889d51a1 NL |
376 | do_utime(vcollected, mtime); |
377 | kfree(vcollected); | |
1da177e4 LT |
378 | eat(body_len); |
379 | state = SkipIt; | |
380 | return 0; | |
381 | } else { | |
c34d85ac | 382 | if (xwrite(wfd, victim, byte_count) != byte_count) |
9687fd91 | 383 | error("write error"); |
c34d85ac MR |
384 | body_len -= byte_count; |
385 | eat(byte_count); | |
1da177e4 LT |
386 | return 1; |
387 | } | |
388 | } | |
389 | ||
390 | static int __init do_symlink(void) | |
391 | { | |
392 | collected[N_ALIGN(name_len) + body_len] = '\0'; | |
2139a7fb | 393 | clean_path(collected, 0); |
1da177e4 LT |
394 | sys_symlink(collected + N_ALIGN(name_len), collected); |
395 | sys_lchown(collected, uid, gid); | |
889d51a1 | 396 | do_utime(collected, mtime); |
1da177e4 LT |
397 | state = SkipIt; |
398 | next_state = Reset; | |
399 | return 0; | |
400 | } | |
401 | ||
402 | static __initdata int (*actions[])(void) = { | |
403 | [Start] = do_start, | |
404 | [Collect] = do_collect, | |
405 | [GotHeader] = do_header, | |
406 | [SkipIt] = do_skip, | |
407 | [GotName] = do_name, | |
408 | [CopyFile] = do_copy, | |
409 | [GotSymlink] = do_symlink, | |
410 | [Reset] = do_reset, | |
411 | }; | |
412 | ||
d97b07c5 | 413 | static long __init write_buffer(char *buf, unsigned long len) |
1da177e4 | 414 | { |
c34d85ac | 415 | byte_count = len; |
1da177e4 LT |
416 | victim = buf; |
417 | ||
418 | while (!actions[state]()) | |
419 | ; | |
c34d85ac | 420 | return len - byte_count; |
1da177e4 LT |
421 | } |
422 | ||
d97b07c5 | 423 | static long __init flush_buffer(void *bufv, unsigned long len) |
1da177e4 | 424 | { |
30d65dbf | 425 | char *buf = (char *) bufv; |
d97b07c5 YL |
426 | long written; |
427 | long origLen = len; | |
1da177e4 | 428 | if (message) |
30d65dbf | 429 | return -1; |
1da177e4 LT |
430 | while ((written = write_buffer(buf, len)) < len && !message) { |
431 | char c = buf[written]; | |
432 | if (c == '0') { | |
433 | buf += written; | |
434 | len -= written; | |
435 | state = Start; | |
436 | } else if (c == 0) { | |
437 | buf += written; | |
438 | len -= written; | |
439 | state = Reset; | |
440 | } else | |
441 | error("junk in compressed archive"); | |
442 | } | |
30d65dbf | 443 | return origLen; |
1da177e4 LT |
444 | } |
445 | ||
d97b07c5 | 446 | static unsigned long my_inptr; /* index of next byte to be processed in inbuf */ |
1da177e4 | 447 | |
889c92d2 | 448 | #include <linux/decompress/generic.h> |
1da177e4 | 449 | |
d97b07c5 | 450 | static char * __init unpack_to_rootfs(char *buf, unsigned long len) |
1da177e4 | 451 | { |
d97b07c5 | 452 | long written; |
889c92d2 | 453 | decompress_fn decompress; |
23a22d57 PA |
454 | const char *compress_name; |
455 | static __initdata char msg_buf[64]; | |
889c92d2 | 456 | |
3265e66b TP |
457 | header_buf = kmalloc(110, GFP_KERNEL); |
458 | symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); | |
459 | name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); | |
30d65dbf AK |
460 | |
461 | if (!header_buf || !symlink_buf || !name_buf) | |
1da177e4 | 462 | panic("can't allocate buffers"); |
30d65dbf | 463 | |
1da177e4 LT |
464 | state = Start; |
465 | this_header = 0; | |
466 | message = NULL; | |
467 | while (!message && len) { | |
468 | loff_t saved_offset = this_header; | |
469 | if (*buf == '0' && !(this_header & 3)) { | |
470 | state = Start; | |
471 | written = write_buffer(buf, len); | |
472 | buf += written; | |
473 | len -= written; | |
474 | continue; | |
475 | } | |
476 | if (!*buf) { | |
477 | buf++; | |
478 | len--; | |
479 | this_header++; | |
480 | continue; | |
481 | } | |
482 | this_header = 0; | |
23a22d57 | 483 | decompress = decompress_method(buf, len, &compress_name); |
6aa7a29a | 484 | pr_debug("Detected %s compressed data\n", compress_name); |
54291362 | 485 | if (decompress) { |
d97b07c5 | 486 | int res = decompress(buf, len, NULL, flush_buffer, NULL, |
889c92d2 | 487 | &my_inptr, error); |
54291362 PL |
488 | if (res) |
489 | error("decompressor failed"); | |
490 | } else if (compress_name) { | |
23a22d57 PA |
491 | if (!message) { |
492 | snprintf(msg_buf, sizeof msg_buf, | |
493 | "compression method %s not configured", | |
494 | compress_name); | |
495 | message = msg_buf; | |
496 | } | |
df37bd15 PL |
497 | } else |
498 | error("junk in compressed archive"); | |
1da177e4 | 499 | if (state != Reset) |
30d65dbf AK |
500 | error("junk in compressed archive"); |
501 | this_header = saved_offset + my_inptr; | |
502 | buf += my_inptr; | |
503 | len -= my_inptr; | |
1da177e4 | 504 | } |
889d51a1 | 505 | dir_utime(); |
3265e66b TP |
506 | kfree(name_buf); |
507 | kfree(symlink_buf); | |
508 | kfree(header_buf); | |
1da177e4 LT |
509 | return message; |
510 | } | |
511 | ||
0a7b35cb MN |
512 | static int __initdata do_retain_initrd; |
513 | ||
514 | static int __init retain_initrd_param(char *str) | |
515 | { | |
516 | if (*str) | |
517 | return 0; | |
518 | do_retain_initrd = 1; | |
519 | return 1; | |
520 | } | |
521 | __setup("retain_initrd", retain_initrd_param); | |
522 | ||
ffe8018c HB |
523 | extern char __initramfs_start[]; |
524 | extern unsigned long __initramfs_size; | |
1da177e4 | 525 | #include <linux/initrd.h> |
9c15e852 | 526 | #include <linux/kexec.h> |
0f3d2bd5 JB |
527 | |
528 | static void __init free_initrd(void) | |
529 | { | |
2965faa5 | 530 | #ifdef CONFIG_KEXEC_CORE |
9c15e852 HM |
531 | unsigned long crashk_start = (unsigned long)__va(crashk_res.start); |
532 | unsigned long crashk_end = (unsigned long)__va(crashk_res.end); | |
0a7b35cb MN |
533 | #endif |
534 | if (do_retain_initrd) | |
535 | goto skip; | |
9c15e852 | 536 | |
2965faa5 | 537 | #ifdef CONFIG_KEXEC_CORE |
9c15e852 HM |
538 | /* |
539 | * If the initrd region is overlapped with crashkernel reserved region, | |
540 | * free only memory that is not part of crashkernel region. | |
541 | */ | |
542 | if (initrd_start < crashk_end && initrd_end > crashk_start) { | |
543 | /* | |
544 | * Initialize initrd memory region since the kexec boot does | |
545 | * not do. | |
546 | */ | |
547 | memset((void *)initrd_start, 0, initrd_end - initrd_start); | |
548 | if (initrd_start < crashk_start) | |
549 | free_initrd_mem(initrd_start, crashk_start); | |
550 | if (initrd_end > crashk_end) | |
551 | free_initrd_mem(crashk_end, initrd_end); | |
552 | } else | |
553 | #endif | |
554 | free_initrd_mem(initrd_start, initrd_end); | |
0a7b35cb | 555 | skip: |
0f3d2bd5 JB |
556 | initrd_start = 0; |
557 | initrd_end = 0; | |
558 | } | |
559 | ||
b52bb371 | 560 | #ifdef CONFIG_BLK_DEV_RAM |
df52092f LS |
561 | #define BUF_SIZE 1024 |
562 | static void __init clean_rootfs(void) | |
563 | { | |
564 | int fd; | |
565 | void *buf; | |
566 | struct linux_dirent64 *dirp; | |
8aaed5be | 567 | int num; |
df52092f | 568 | |
c67e5382 | 569 | fd = sys_open("/", O_RDONLY, 0); |
df52092f LS |
570 | WARN_ON(fd < 0); |
571 | if (fd < 0) | |
572 | return; | |
573 | buf = kzalloc(BUF_SIZE, GFP_KERNEL); | |
574 | WARN_ON(!buf); | |
575 | if (!buf) { | |
576 | sys_close(fd); | |
577 | return; | |
578 | } | |
579 | ||
580 | dirp = buf; | |
8aaed5be HS |
581 | num = sys_getdents64(fd, dirp, BUF_SIZE); |
582 | while (num > 0) { | |
583 | while (num > 0) { | |
df52092f LS |
584 | struct stat st; |
585 | int ret; | |
586 | ||
587 | ret = sys_newlstat(dirp->d_name, &st); | |
588 | WARN_ON_ONCE(ret); | |
589 | if (!ret) { | |
590 | if (S_ISDIR(st.st_mode)) | |
591 | sys_rmdir(dirp->d_name); | |
592 | else | |
593 | sys_unlink(dirp->d_name); | |
594 | } | |
595 | ||
8aaed5be | 596 | num -= dirp->d_reclen; |
df52092f LS |
597 | dirp = (void *)dirp + dirp->d_reclen; |
598 | } | |
599 | dirp = buf; | |
600 | memset(buf, 0, BUF_SIZE); | |
8aaed5be | 601 | num = sys_getdents64(fd, dirp, BUF_SIZE); |
df52092f LS |
602 | } |
603 | ||
604 | sys_close(fd); | |
605 | kfree(buf); | |
606 | } | |
b52bb371 | 607 | #endif |
df52092f | 608 | |
9a9e0d68 | 609 | static int __init populate_rootfs(void) |
1da177e4 | 610 | { |
17a9be31 | 611 | /* Load the built in initramfs */ |
ffe8018c | 612 | char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); |
1da177e4 | 613 | if (err) |
499a4584 | 614 | panic("%s", err); /* Failed to decompress INTERNAL initramfs */ |
17a9be31 | 615 | /* If available load the bootloader supplied initrd */ |
1da177e4 | 616 | if (initrd_start) { |
340e48e6 | 617 | #ifdef CONFIG_BLK_DEV_RAM |
1da177e4 | 618 | int fd; |
a1e6b6c1 | 619 | printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); |
1da177e4 | 620 | err = unpack_to_rootfs((char *)initrd_start, |
df52092f | 621 | initrd_end - initrd_start); |
1da177e4 | 622 | if (!err) { |
0f3d2bd5 | 623 | free_initrd(); |
bb813f4c | 624 | goto done; |
df52092f LS |
625 | } else { |
626 | clean_rootfs(); | |
ffe8018c | 627 | unpack_to_rootfs(__initramfs_start, __initramfs_size); |
1da177e4 | 628 | } |
c1c490e0 SK |
629 | printk(KERN_INFO "rootfs image is not initramfs (%s)" |
630 | "; looks like an initrd\n", err); | |
c67e5382 | 631 | fd = sys_open("/initrd.image", |
562f5e63 | 632 | O_WRONLY|O_CREAT, 0700); |
1da177e4 | 633 | if (fd >= 0) { |
38747439 YL |
634 | ssize_t written = xwrite(fd, (char *)initrd_start, |
635 | initrd_end - initrd_start); | |
636 | ||
637 | if (written != initrd_end - initrd_start) | |
638 | pr_err("/initrd.image: incomplete write (%zd != %ld)\n", | |
639 | written, initrd_end - initrd_start); | |
640 | ||
1da177e4 | 641 | sys_close(fd); |
0f3d2bd5 | 642 | free_initrd(); |
1da177e4 | 643 | } |
bb813f4c | 644 | done: |
394e4f5d | 645 | /* empty statement */; |
340e48e6 | 646 | #else |
a1e6b6c1 | 647 | printk(KERN_INFO "Unpacking initramfs...\n"); |
340e48e6 | 648 | err = unpack_to_rootfs((char *)initrd_start, |
df52092f | 649 | initrd_end - initrd_start); |
a1e6b6c1 EP |
650 | if (err) |
651 | printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); | |
340e48e6 ZP |
652 | free_initrd(); |
653 | #endif | |
1da177e4 | 654 | } |
17a9be31 SH |
655 | flush_delayed_fput(); |
656 | /* | |
657 | * Try loading default modules from initramfs. This gives | |
658 | * us a chance to load before device_initcalls. | |
659 | */ | |
660 | load_default_modules(); | |
661 | ||
8d610dd5 | 662 | return 0; |
1da177e4 | 663 | } |
8d610dd5 | 664 | rootfs_initcall(populate_rootfs); |