blob: 8a532050043f5f804f06ddbf980e54965c9055de [file] [log] [blame]
H Hartley Sweetenc67e5382012-05-31 16:26:10 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Li, Shaohuadf520922008-08-13 17:26:01 +080018#include <linux/dirent.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/syscalls.h>
Nye Liu889d51a2008-10-15 22:01:40 -070020#include <linux/utime.h>
Lokesh Vutla08865512017-02-27 14:28:12 -080021#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Yinghai Lu38747432014-08-08 14:23:12 -070023static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static __initdata char *message;
47static void __init error(char *x)
48{
49 if (!message)
50 message = x;
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* link hash */
54
Mark Huang6a050da2006-05-15 09:44:03 -070055#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static __initdata struct hash {
58 int ino, minor, major;
Al Viro685dd2d2011-07-26 04:34:13 -040059 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct hash *next;
Mark Huang6a050da2006-05-15 09:44:03 -070061 char name[N_ALIGN(PATH_MAX)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062} *head[32];
63
64static 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
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070071static char __init *find_link(int major, int minor, int ino,
Al Viro685dd2d2011-07-26 04:34:13 -040072 umode_t mode, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
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;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070082 if (((*p)->mode ^ mode) & S_IFMT)
83 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return (*p)->name;
85 }
Thomas Petazzoni3265e662008-04-29 00:59:43 -070086 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (!q)
88 panic("can't allocate link hash entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 q->major = major;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070090 q->minor = minor;
91 q->ino = ino;
92 q->mode = mode;
Mark Huang6a050da2006-05-15 09:44:03 -070093 strcpy(q->name, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 q->next = NULL;
95 *p = q;
96 return NULL;
97}
98
99static 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;
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700106 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 }
109}
110
H Hartley Sweetenc67e5382012-05-31 16:26:10 -0700111static long __init do_utime(char *filename, time_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700112{
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
123static __initdata LIST_HEAD(dir_list);
124struct dir_entry {
125 struct list_head list;
126 char *name;
127 time_t mtime;
128};
129
130static 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
141static 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
152static __initdata time_t mtime;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/* cpio header parsing */
155
156static __initdata unsigned long ino, major, minor, nlink;
Al Viro685dd2d2011-07-26 04:34:13 -0400157static __initdata umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static __initdata unsigned long body_len, name_len;
159static __initdata uid_t uid;
160static __initdata gid_t gid;
161static __initdata unsigned rdev;
162
163static 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];
Nye Liu889d51a2008-10-15 22:01:40 -0700179 mtime = parsed[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 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
189static __initdata enum state {
190 Start,
191 Collect,
192 GotHeader,
193 SkipIt,
194 GotName,
195 CopyFile,
196 GotSymlink,
197 Reset
198} state, next_state;
199
200static __initdata char *victim;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700201static unsigned long byte_count __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static __initdata loff_t this_header, next_header;
203
Al Virob0a5ab92007-07-26 17:33:59 +0100204static inline void __init eat(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 victim += n;
207 this_header += n;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700208 byte_count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Nye Liu889d51a2008-10-15 22:01:40 -0700211static __initdata char *vcollected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static __initdata char *collected;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700213static long remains __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static __initdata char *collect;
215
216static void __init read_into(char *buf, unsigned size, enum state next)
217{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700218 if (byte_count >= size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 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
230static __initdata char *header_buf, *symlink_buf, *name_buf;
231
232static int __init do_start(void)
233{
234 read_into(header_buf, 110, GotHeader);
235 return 0;
236}
237
238static int __init do_collect(void)
239{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700240 unsigned long n = remains;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700241 if (byte_count < n)
242 n = byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 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
252static int __init do_header(void)
253{
Arjan van de Ven2e591bb2006-12-06 20:37:19 -0800254 if (memcmp(collected, "070707", 6)==0) {
255 error("incorrect cpio method used: use -H newc option");
256 return 1;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 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
282static int __init do_skip(void)
283{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700284 if (this_header + byte_count < next_header) {
285 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 1;
287 } else {
288 eat(next_header - this_header);
289 state = next_state;
290 return 0;
291 }
292}
293
294static int __init do_reset(void)
295{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700296 while (byte_count && *victim == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 eat(1);
Mark Rustadc34d85a2014-10-13 15:54:07 -0700298 if (byte_count && (this_header & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 error("broken padding");
300 return 1;
301}
302
303static int __init maybe_link(void)
304{
305 if (nlink >= 2) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700306 char *old = find_link(major, minor, ino, mode, collected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (old)
308 return (sys_link(old, collected) < 0) ? -1 : 1;
309 }
310 return 0;
311}
312
Mark Rustadc34d85a2014-10-13 15:54:07 -0700313static void __init clean_path(char *path, umode_t fmode)
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700314{
Arnd Bergmann046aa122017-05-08 15:57:00 -0700315 struct kstat st;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700316
Arnd Bergmann046aa122017-05-08 15:57:00 -0700317 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
318 if (S_ISDIR(st.mode))
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700319 sys_rmdir(path);
320 else
321 sys_unlink(path);
322 }
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static __initdata int wfd;
326
327static 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 }
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700335 clean_path(collected, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (S_ISREG(mode)) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700337 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (wfd >= 0) {
345 sys_fchown(wfd, uid, gid);
346 sys_fchmod(wfd, mode);
Randy Robertsond20d5a72009-04-13 14:40:04 -0700347 if (body_len)
348 sys_ftruncate(wfd, body_len);
Nye Liu889d51a2008-10-15 22:01:40 -0700349 vcollected = kstrdup(collected, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 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);
Nye Liu889d51a2008-10-15 22:01:40 -0700357 dir_add(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 } 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);
Nye Liu889d51a2008-10-15 22:01:40 -0700364 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 }
367 return 0;
368}
369
370static int __init do_copy(void)
371{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700372 if (byte_count >= body_len) {
David Engraf9687fd92014-08-08 14:23:16 -0700373 if (xwrite(wfd, victim, body_len) != body_len)
374 error("write error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sys_close(wfd);
Nye Liu889d51a2008-10-15 22:01:40 -0700376 do_utime(vcollected, mtime);
377 kfree(vcollected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 eat(body_len);
379 state = SkipIt;
380 return 0;
381 } else {
Mark Rustadc34d85a2014-10-13 15:54:07 -0700382 if (xwrite(wfd, victim, byte_count) != byte_count)
David Engraf9687fd92014-08-08 14:23:16 -0700383 error("write error");
Mark Rustadc34d85a2014-10-13 15:54:07 -0700384 body_len -= byte_count;
385 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 1;
387 }
388}
389
390static int __init do_symlink(void)
391{
392 collected[N_ALIGN(name_len) + body_len] = '\0';
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700393 clean_path(collected, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sys_symlink(collected + N_ALIGN(name_len), collected);
395 sys_lchown(collected, uid, gid);
Nye Liu889d51a2008-10-15 22:01:40 -0700396 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 state = SkipIt;
398 next_state = Reset;
399 return 0;
400}
401
402static __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
Yinghai Lud97b07c2014-08-08 14:23:14 -0700413static long __init write_buffer(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700415 byte_count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 victim = buf;
417
418 while (!actions[state]())
419 ;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700420 return len - byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Yinghai Lud97b07c2014-08-08 14:23:14 -0700423static long __init flush_buffer(void *bufv, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Alain Knaff30d65db2009-01-04 22:46:17 +0100425 char *buf = (char *) bufv;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700426 long written;
427 long origLen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (message)
Alain Knaff30d65db2009-01-04 22:46:17 +0100429 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 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 }
Alain Knaff30d65db2009-01-04 22:46:17 +0100443 return origLen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Yinghai Lud97b07c2014-08-08 14:23:14 -0700446static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800448#include <linux/decompress/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Yinghai Lud97b07c2014-08-08 14:23:14 -0700450static char * __init unpack_to_rootfs(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700452 long written;
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800453 decompress_fn decompress;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800454 const char *compress_name;
455 static __initdata char msg_buf[64];
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800456
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700457 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);
Alain Knaff30d65db2009-01-04 22:46:17 +0100460
461 if (!header_buf || !symlink_buf || !name_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 panic("can't allocate buffers");
Alain Knaff30d65db2009-01-04 22:46:17 +0100463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 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;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800483 decompress = decompress_method(buf, len, &compress_name);
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -0700484 pr_debug("Detected %s compressed data\n", compress_name);
Phillip Lougher54291362009-12-14 21:45:19 +0000485 if (decompress) {
Yinghai Lud97b07c2014-08-08 14:23:14 -0700486 int res = decompress(buf, len, NULL, flush_buffer, NULL,
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800487 &my_inptr, error);
Phillip Lougher54291362009-12-14 21:45:19 +0000488 if (res)
489 error("decompressor failed");
490 } else if (compress_name) {
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800491 if (!message) {
492 snprintf(msg_buf, sizeof msg_buf,
493 "compression method %s not configured",
494 compress_name);
495 message = msg_buf;
496 }
Phillip Lougherdf37bd12010-04-23 13:18:11 -0400497 } else
498 error("junk in compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (state != Reset)
Alain Knaff30d65db2009-01-04 22:46:17 +0100500 error("junk in compressed archive");
501 this_header = saved_offset + my_inptr;
502 buf += my_inptr;
503 len -= my_inptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Nye Liu889d51a2008-10-15 22:01:40 -0700505 dir_utime();
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700506 kfree(name_buf);
507 kfree(symlink_buf);
508 kfree(header_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return message;
510}
511
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800512static int __initdata do_retain_initrd;
513
514static 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
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700523extern char __initramfs_start[];
524extern unsigned long __initramfs_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#include <linux/initrd.h>
Haren Myneni9c15e852006-02-10 01:51:05 -0800526#include <linux/kexec.h>
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700527
528static void __init free_initrd(void)
529{
Dave Young2965faa2015-09-09 15:38:55 -0700530#ifdef CONFIG_KEXEC_CORE
Haren Myneni9c15e852006-02-10 01:51:05 -0800531 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
532 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800533#endif
534 if (do_retain_initrd)
535 goto skip;
Haren Myneni9c15e852006-02-10 01:51:05 -0800536
Dave Young2965faa2015-09-09 15:38:55 -0700537#ifdef CONFIG_KEXEC_CORE
Haren Myneni9c15e852006-02-10 01:51:05 -0800538 /*
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);
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800555skip:
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700556 initrd_start = 0;
557 initrd_end = 0;
558}
559
Nikanth Karthikesanb52bb372009-04-13 14:39:38 -0700560#ifdef CONFIG_BLK_DEV_RAM
Li, Shaohuadf520922008-08-13 17:26:01 +0800561#define BUF_SIZE 1024
562static void __init clean_rootfs(void)
563{
564 int fd;
565 void *buf;
566 struct linux_dirent64 *dirp;
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800567 int num;
Li, Shaohuadf520922008-08-13 17:26:01 +0800568
H Hartley Sweetenc67e5382012-05-31 16:26:10 -0700569 fd = sys_open("/", O_RDONLY, 0);
Li, Shaohuadf520922008-08-13 17:26:01 +0800570 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;
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800581 num = sys_getdents64(fd, dirp, BUF_SIZE);
582 while (num > 0) {
583 while (num > 0) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700584 struct kstat st;
Li, Shaohuadf520922008-08-13 17:26:01 +0800585 int ret;
586
Arnd Bergmann046aa122017-05-08 15:57:00 -0700587 ret = vfs_lstat(dirp->d_name, &st);
Li, Shaohuadf520922008-08-13 17:26:01 +0800588 WARN_ON_ONCE(ret);
589 if (!ret) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700590 if (S_ISDIR(st.mode))
Li, Shaohuadf520922008-08-13 17:26:01 +0800591 sys_rmdir(dirp->d_name);
592 else
593 sys_unlink(dirp->d_name);
594 }
595
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800596 num -= dirp->d_reclen;
Li, Shaohuadf520922008-08-13 17:26:01 +0800597 dirp = (void *)dirp + dirp->d_reclen;
598 }
599 dirp = buf;
600 memset(buf, 0, BUF_SIZE);
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800601 num = sys_getdents64(fd, dirp, BUF_SIZE);
Li, Shaohuadf520922008-08-13 17:26:01 +0800602 }
603
604 sys_close(fd);
605 kfree(buf);
606}
Nikanth Karthikesanb52bb372009-04-13 14:39:38 -0700607#endif
Li, Shaohuadf520922008-08-13 17:26:01 +0800608
Linus Torvalds9a9e0d62008-03-15 11:53:32 -0700609static int __init populate_rootfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Stafford Horne17a9be32017-05-04 21:15:56 +0900611 /* Load the built in initramfs */
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700612 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (err)
Tetsuo Handa499a4582014-01-23 15:54:56 -0800614 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
Stafford Horne17a9be32017-05-04 21:15:56 +0900615 /* If available load the bootloader supplied initrd */
Daniel Thompsoncff75e02017-05-08 15:56:57 -0700616 if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800617#ifdef CONFIG_BLK_DEV_RAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int fd;
Eric Piela1e6b6c2009-05-06 16:03:06 -0700619 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 err = unpack_to_rootfs((char *)initrd_start,
Li, Shaohuadf520922008-08-13 17:26:01 +0800621 initrd_end - initrd_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!err) {
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700623 free_initrd();
Tejun Heobb813f42013-01-18 14:05:56 -0800624 goto done;
Li, Shaohuadf520922008-08-13 17:26:01 +0800625 } else {
626 clean_rootfs();
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700627 unpack_to_rootfs(__initramfs_start, __initramfs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Simon Kitchingc1c490e2009-04-02 16:57:00 -0700629 printk(KERN_INFO "rootfs image is not initramfs (%s)"
630 "; looks like an initrd\n", err);
H Hartley Sweetenc67e5382012-05-31 16:26:10 -0700631 fd = sys_open("/initrd.image",
Namhyung Kim562f5e62010-10-26 14:22:42 -0700632 O_WRONLY|O_CREAT, 0700);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (fd >= 0) {
Yinghai Lu38747432014-08-08 14:23:12 -0700634 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 sys_close(fd);
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700642 free_initrd();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Tejun Heobb813f42013-01-18 14:05:56 -0800644 done:
Linus Torvalds394e4f52017-05-06 10:27:13 -0700645 /* empty statement */;
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800646#else
Eric Piela1e6b6c2009-05-06 16:03:06 -0700647 printk(KERN_INFO "Unpacking initramfs...\n");
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800648 err = unpack_to_rootfs((char *)initrd_start,
Li, Shaohuadf520922008-08-13 17:26:01 +0800649 initrd_end - initrd_start);
Eric Piela1e6b6c2009-05-06 16:03:06 -0700650 if (err)
651 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800652 free_initrd();
653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Stafford Horne17a9be32017-05-04 21:15:56 +0900655 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
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800664rootfs_initcall(populate_rootfs);