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