blob: 7e99a00389429d79281ac2663a6fb7a4bedaef1d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
H Hartley Sweetenc67e5382012-05-31 16:26:10 -07002/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Li, Shaohuadf520922008-08-13 17:26:01 +080019#include <linux/dirent.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/syscalls.h>
Nye Liu889d51a2008-10-15 22:01:40 -070021#include <linux/utime.h>
Lokesh Vutla08865512017-02-27 14:28:12 -080022#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Yinghai Lu38747432014-08-08 14:23:12 -070024static ssize_t __init xwrite(int fd, const char *p, size_t count)
25{
26 ssize_t out = 0;
27
28 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
29 while (count) {
30 ssize_t rv = sys_write(fd, p, count);
31
32 if (rv < 0) {
33 if (rv == -EINTR || rv == -EAGAIN)
34 continue;
35 return out ? out : rv;
36 } else if (rv == 0)
37 break;
38
39 p += rv;
40 out += rv;
41 count -= rv;
42 }
43
44 return out;
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static __initdata char *message;
48static void __init error(char *x)
49{
50 if (!message)
51 message = x;
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* link hash */
55
Mark Huang6a050da2006-05-15 09:44:03 -070056#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static __initdata struct hash {
59 int ino, minor, major;
Al Viro685dd2d2011-07-26 04:34:13 -040060 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct hash *next;
Mark Huang6a050da2006-05-15 09:44:03 -070062 char name[N_ALIGN(PATH_MAX)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063} *head[32];
64
65static inline int hash(int major, int minor, int ino)
66{
67 unsigned long tmp = ino + minor + (major << 3);
68 tmp += tmp >> 5;
69 return tmp & 31;
70}
71
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070072static char __init *find_link(int major, int minor, int ino,
Al Viro685dd2d2011-07-26 04:34:13 -040073 umode_t mode, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 struct hash **p, *q;
76 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
77 if ((*p)->ino != ino)
78 continue;
79 if ((*p)->minor != minor)
80 continue;
81 if ((*p)->major != major)
82 continue;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070083 if (((*p)->mode ^ mode) & S_IFMT)
84 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return (*p)->name;
86 }
Thomas Petazzoni3265e662008-04-29 00:59:43 -070087 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!q)
89 panic("can't allocate link hash entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 q->major = major;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070091 q->minor = minor;
92 q->ino = ino;
93 q->mode = mode;
Mark Huang6a050da2006-05-15 09:44:03 -070094 strcpy(q->name, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 q->next = NULL;
96 *p = q;
97 return NULL;
98}
99
100static void __init free_hash(void)
101{
102 struct hash **p, *q;
103 for (p = head; p < head + 32; p++) {
104 while (*p) {
105 q = *p;
106 *p = q->next;
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700107 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 }
110}
111
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800112static long __init do_utime(char *filename, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700113{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700114 struct timespec64 t[2];
Nye Liu889d51a2008-10-15 22:01:40 -0700115
116 t[0].tv_sec = mtime;
117 t[0].tv_nsec = 0;
118 t[1].tv_sec = mtime;
119 t[1].tv_nsec = 0;
120
121 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
122}
123
124static __initdata LIST_HEAD(dir_list);
125struct dir_entry {
126 struct list_head list;
127 char *name;
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800128 time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700129};
130
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800131static void __init dir_add(const char *name, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700132{
133 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
134 if (!de)
135 panic("can't allocate dir_entry buffer");
136 INIT_LIST_HEAD(&de->list);
137 de->name = kstrdup(name, GFP_KERNEL);
138 de->mtime = mtime;
139 list_add(&de->list, &dir_list);
140}
141
142static void __init dir_utime(void)
143{
144 struct dir_entry *de, *tmp;
145 list_for_each_entry_safe(de, tmp, &dir_list, list) {
146 list_del(&de->list);
147 do_utime(de->name, de->mtime);
148 kfree(de->name);
149 kfree(de);
150 }
151}
152
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800153static __initdata time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* cpio header parsing */
156
157static __initdata unsigned long ino, major, minor, nlink;
Al Viro685dd2d2011-07-26 04:34:13 -0400158static __initdata umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static __initdata unsigned long body_len, name_len;
160static __initdata uid_t uid;
161static __initdata gid_t gid;
162static __initdata unsigned rdev;
163
164static void __init parse_header(char *s)
165{
166 unsigned long parsed[12];
167 char buf[9];
168 int i;
169
170 buf[8] = '\0';
171 for (i = 0, s += 6; i < 12; i++, s += 8) {
172 memcpy(buf, s, 8);
173 parsed[i] = simple_strtoul(buf, NULL, 16);
174 }
175 ino = parsed[0];
176 mode = parsed[1];
177 uid = parsed[2];
178 gid = parsed[3];
179 nlink = parsed[4];
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800180 mtime = parsed[5]; /* breaks in y2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 body_len = parsed[6];
182 major = parsed[7];
183 minor = parsed[8];
184 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
185 name_len = parsed[11];
186}
187
188/* FSM */
189
190static __initdata enum state {
191 Start,
192 Collect,
193 GotHeader,
194 SkipIt,
195 GotName,
196 CopyFile,
197 GotSymlink,
198 Reset
199} state, next_state;
200
201static __initdata char *victim;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700202static unsigned long byte_count __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static __initdata loff_t this_header, next_header;
204
Al Virob0a5ab92007-07-26 17:33:59 +0100205static inline void __init eat(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 victim += n;
208 this_header += n;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700209 byte_count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Nye Liu889d51a2008-10-15 22:01:40 -0700212static __initdata char *vcollected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static __initdata char *collected;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700214static long remains __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static __initdata char *collect;
216
217static void __init read_into(char *buf, unsigned size, enum state next)
218{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700219 if (byte_count >= size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 collected = victim;
221 eat(size);
222 state = next;
223 } else {
224 collect = collected = buf;
225 remains = size;
226 next_state = next;
227 state = Collect;
228 }
229}
230
231static __initdata char *header_buf, *symlink_buf, *name_buf;
232
233static int __init do_start(void)
234{
235 read_into(header_buf, 110, GotHeader);
236 return 0;
237}
238
239static int __init do_collect(void)
240{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700241 unsigned long n = remains;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700242 if (byte_count < n)
243 n = byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 memcpy(collect, victim, n);
245 eat(n);
246 collect += n;
247 if ((remains -= n) != 0)
248 return 1;
249 state = next_state;
250 return 0;
251}
252
253static int __init do_header(void)
254{
Arjan van de Ven2e591bb2006-12-06 20:37:19 -0800255 if (memcmp(collected, "070707", 6)==0) {
256 error("incorrect cpio method used: use -H newc option");
257 return 1;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (memcmp(collected, "070701", 6)) {
260 error("no cpio magic");
261 return 1;
262 }
263 parse_header(collected);
264 next_header = this_header + N_ALIGN(name_len) + body_len;
265 next_header = (next_header + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 state = SkipIt;
267 if (name_len <= 0 || name_len > PATH_MAX)
268 return 0;
269 if (S_ISLNK(mode)) {
270 if (body_len > PATH_MAX)
271 return 0;
272 collect = collected = symlink_buf;
273 remains = N_ALIGN(name_len) + body_len;
274 next_state = GotSymlink;
275 state = Collect;
276 return 0;
277 }
278 if (S_ISREG(mode) || !body_len)
279 read_into(name_buf, N_ALIGN(name_len), GotName);
280 return 0;
281}
282
283static int __init do_skip(void)
284{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700285 if (this_header + byte_count < next_header) {
286 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 1;
288 } else {
289 eat(next_header - this_header);
290 state = next_state;
291 return 0;
292 }
293}
294
295static int __init do_reset(void)
296{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700297 while (byte_count && *victim == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 eat(1);
Mark Rustadc34d85a2014-10-13 15:54:07 -0700299 if (byte_count && (this_header & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 error("broken padding");
301 return 1;
302}
303
304static int __init maybe_link(void)
305{
306 if (nlink >= 2) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700307 char *old = find_link(major, minor, ino, mode, collected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (old)
309 return (sys_link(old, collected) < 0) ? -1 : 1;
310 }
311 return 0;
312}
313
Mark Rustadc34d85a2014-10-13 15:54:07 -0700314static void __init clean_path(char *path, umode_t fmode)
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700315{
Arnd Bergmann046aa122017-05-08 15:57:00 -0700316 struct kstat st;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700317
Arnd Bergmann046aa122017-05-08 15:57:00 -0700318 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
319 if (S_ISDIR(st.mode))
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700320 sys_rmdir(path);
321 else
322 sys_unlink(path);
323 }
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static __initdata int wfd;
327
328static int __init do_name(void)
329{
330 state = SkipIt;
331 next_state = Reset;
332 if (strcmp(collected, "TRAILER!!!") == 0) {
333 free_hash();
334 return 0;
335 }
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700336 clean_path(collected, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (S_ISREG(mode)) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700338 int ml = maybe_link();
339 if (ml >= 0) {
340 int openflags = O_WRONLY|O_CREAT;
341 if (ml != 1)
342 openflags |= O_TRUNC;
343 wfd = sys_open(collected, openflags, mode);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (wfd >= 0) {
346 sys_fchown(wfd, uid, gid);
347 sys_fchmod(wfd, mode);
Randy Robertsond20d5a72009-04-13 14:40:04 -0700348 if (body_len)
349 sys_ftruncate(wfd, body_len);
Nye Liu889d51a2008-10-15 22:01:40 -0700350 vcollected = kstrdup(collected, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 state = CopyFile;
352 }
353 }
354 } else if (S_ISDIR(mode)) {
355 sys_mkdir(collected, mode);
356 sys_chown(collected, uid, gid);
357 sys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700358 dir_add(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
360 S_ISFIFO(mode) || S_ISSOCK(mode)) {
361 if (maybe_link() == 0) {
362 sys_mknod(collected, mode, rdev);
363 sys_chown(collected, uid, gid);
364 sys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700365 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 }
368 return 0;
369}
370
371static int __init do_copy(void)
372{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700373 if (byte_count >= body_len) {
David Engraf9687fd92014-08-08 14:23:16 -0700374 if (xwrite(wfd, victim, body_len) != body_len)
375 error("write error");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 sys_close(wfd);
Nye Liu889d51a2008-10-15 22:01:40 -0700377 do_utime(vcollected, mtime);
378 kfree(vcollected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 eat(body_len);
380 state = SkipIt;
381 return 0;
382 } else {
Mark Rustadc34d85a2014-10-13 15:54:07 -0700383 if (xwrite(wfd, victim, byte_count) != byte_count)
David Engraf9687fd92014-08-08 14:23:16 -0700384 error("write error");
Mark Rustadc34d85a2014-10-13 15:54:07 -0700385 body_len -= byte_count;
386 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 1;
388 }
389}
390
391static int __init do_symlink(void)
392{
393 collected[N_ALIGN(name_len) + body_len] = '\0';
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700394 clean_path(collected, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sys_symlink(collected + N_ALIGN(name_len), collected);
396 sys_lchown(collected, uid, gid);
Nye Liu889d51a2008-10-15 22:01:40 -0700397 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 state = SkipIt;
399 next_state = Reset;
400 return 0;
401}
402
403static __initdata int (*actions[])(void) = {
404 [Start] = do_start,
405 [Collect] = do_collect,
406 [GotHeader] = do_header,
407 [SkipIt] = do_skip,
408 [GotName] = do_name,
409 [CopyFile] = do_copy,
410 [GotSymlink] = do_symlink,
411 [Reset] = do_reset,
412};
413
Yinghai Lud97b07c2014-08-08 14:23:14 -0700414static long __init write_buffer(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700416 byte_count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 victim = buf;
418
419 while (!actions[state]())
420 ;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700421 return len - byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Yinghai Lud97b07c2014-08-08 14:23:14 -0700424static long __init flush_buffer(void *bufv, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Alain Knaff30d65db2009-01-04 22:46:17 +0100426 char *buf = (char *) bufv;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700427 long written;
428 long origLen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (message)
Alain Knaff30d65db2009-01-04 22:46:17 +0100430 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 while ((written = write_buffer(buf, len)) < len && !message) {
432 char c = buf[written];
433 if (c == '0') {
434 buf += written;
435 len -= written;
436 state = Start;
437 } else if (c == 0) {
438 buf += written;
439 len -= written;
440 state = Reset;
441 } else
442 error("junk in compressed archive");
443 }
Alain Knaff30d65db2009-01-04 22:46:17 +0100444 return origLen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Yinghai Lud97b07c2014-08-08 14:23:14 -0700447static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800449#include <linux/decompress/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Yinghai Lud97b07c2014-08-08 14:23:14 -0700451static char * __init unpack_to_rootfs(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700453 long written;
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800454 decompress_fn decompress;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800455 const char *compress_name;
456 static __initdata char msg_buf[64];
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800457
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700458 header_buf = kmalloc(110, GFP_KERNEL);
459 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
460 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
Alain Knaff30d65db2009-01-04 22:46:17 +0100461
462 if (!header_buf || !symlink_buf || !name_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 panic("can't allocate buffers");
Alain Knaff30d65db2009-01-04 22:46:17 +0100464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 state = Start;
466 this_header = 0;
467 message = NULL;
468 while (!message && len) {
469 loff_t saved_offset = this_header;
470 if (*buf == '0' && !(this_header & 3)) {
471 state = Start;
472 written = write_buffer(buf, len);
473 buf += written;
474 len -= written;
475 continue;
476 }
477 if (!*buf) {
478 buf++;
479 len--;
480 this_header++;
481 continue;
482 }
483 this_header = 0;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800484 decompress = decompress_method(buf, len, &compress_name);
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -0700485 pr_debug("Detected %s compressed data\n", compress_name);
Phillip Lougher54291362009-12-14 21:45:19 +0000486 if (decompress) {
Yinghai Lud97b07c2014-08-08 14:23:14 -0700487 int res = decompress(buf, len, NULL, flush_buffer, NULL,
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800488 &my_inptr, error);
Phillip Lougher54291362009-12-14 21:45:19 +0000489 if (res)
490 error("decompressor failed");
491 } else if (compress_name) {
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800492 if (!message) {
493 snprintf(msg_buf, sizeof msg_buf,
494 "compression method %s not configured",
495 compress_name);
496 message = msg_buf;
497 }
Phillip Lougherdf37bd12010-04-23 13:18:11 -0400498 } else
499 error("junk in compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (state != Reset)
Alain Knaff30d65db2009-01-04 22:46:17 +0100501 error("junk in compressed archive");
502 this_header = saved_offset + my_inptr;
503 buf += my_inptr;
504 len -= my_inptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Nye Liu889d51a2008-10-15 22:01:40 -0700506 dir_utime();
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700507 kfree(name_buf);
508 kfree(symlink_buf);
509 kfree(header_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return message;
511}
512
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800513static int __initdata do_retain_initrd;
514
515static int __init retain_initrd_param(char *str)
516{
517 if (*str)
518 return 0;
519 do_retain_initrd = 1;
520 return 1;
521}
522__setup("retain_initrd", retain_initrd_param);
523
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700524extern char __initramfs_start[];
525extern unsigned long __initramfs_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#include <linux/initrd.h>
Haren Myneni9c15e852006-02-10 01:51:05 -0800527#include <linux/kexec.h>
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700528
529static void __init free_initrd(void)
530{
Dave Young2965faa2015-09-09 15:38:55 -0700531#ifdef CONFIG_KEXEC_CORE
Haren Myneni9c15e852006-02-10 01:51:05 -0800532 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
533 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800534#endif
535 if (do_retain_initrd)
536 goto skip;
Haren Myneni9c15e852006-02-10 01:51:05 -0800537
Dave Young2965faa2015-09-09 15:38:55 -0700538#ifdef CONFIG_KEXEC_CORE
Haren Myneni9c15e852006-02-10 01:51:05 -0800539 /*
540 * If the initrd region is overlapped with crashkernel reserved region,
541 * free only memory that is not part of crashkernel region.
542 */
543 if (initrd_start < crashk_end && initrd_end > crashk_start) {
544 /*
545 * Initialize initrd memory region since the kexec boot does
546 * not do.
547 */
548 memset((void *)initrd_start, 0, initrd_end - initrd_start);
549 if (initrd_start < crashk_start)
550 free_initrd_mem(initrd_start, crashk_start);
551 if (initrd_end > crashk_end)
552 free_initrd_mem(crashk_end, initrd_end);
553 } else
554#endif
555 free_initrd_mem(initrd_start, initrd_end);
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800556skip:
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700557 initrd_start = 0;
558 initrd_end = 0;
559}
560
Nikanth Karthikesanb52bb372009-04-13 14:39:38 -0700561#ifdef CONFIG_BLK_DEV_RAM
Li, Shaohuadf520922008-08-13 17:26:01 +0800562#define BUF_SIZE 1024
563static void __init clean_rootfs(void)
564{
565 int fd;
566 void *buf;
567 struct linux_dirent64 *dirp;
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800568 int num;
Li, Shaohuadf520922008-08-13 17:26:01 +0800569
H Hartley Sweetenc67e5382012-05-31 16:26:10 -0700570 fd = sys_open("/", O_RDONLY, 0);
Li, Shaohuadf520922008-08-13 17:26:01 +0800571 WARN_ON(fd < 0);
572 if (fd < 0)
573 return;
574 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
575 WARN_ON(!buf);
576 if (!buf) {
577 sys_close(fd);
578 return;
579 }
580
581 dirp = buf;
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800582 num = sys_getdents64(fd, dirp, BUF_SIZE);
583 while (num > 0) {
584 while (num > 0) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700585 struct kstat st;
Li, Shaohuadf520922008-08-13 17:26:01 +0800586 int ret;
587
Arnd Bergmann046aa122017-05-08 15:57:00 -0700588 ret = vfs_lstat(dirp->d_name, &st);
Li, Shaohuadf520922008-08-13 17:26:01 +0800589 WARN_ON_ONCE(ret);
590 if (!ret) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700591 if (S_ISDIR(st.mode))
Li, Shaohuadf520922008-08-13 17:26:01 +0800592 sys_rmdir(dirp->d_name);
593 else
594 sys_unlink(dirp->d_name);
595 }
596
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800597 num -= dirp->d_reclen;
Li, Shaohuadf520922008-08-13 17:26:01 +0800598 dirp = (void *)dirp + dirp->d_reclen;
599 }
600 dirp = buf;
601 memset(buf, 0, BUF_SIZE);
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800602 num = sys_getdents64(fd, dirp, BUF_SIZE);
Li, Shaohuadf520922008-08-13 17:26:01 +0800603 }
604
605 sys_close(fd);
606 kfree(buf);
607}
Nikanth Karthikesanb52bb372009-04-13 14:39:38 -0700608#endif
Li, Shaohuadf520922008-08-13 17:26:01 +0800609
Linus Torvalds9a9e0d62008-03-15 11:53:32 -0700610static int __init populate_rootfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Stafford Horne17a9be32017-05-04 21:15:56 +0900612 /* Load the built in initramfs */
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700613 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (err)
Tetsuo Handa499a4582014-01-23 15:54:56 -0800615 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
Stafford Horne17a9be32017-05-04 21:15:56 +0900616 /* If available load the bootloader supplied initrd */
Daniel Thompsoncff75e02017-05-08 15:56:57 -0700617 if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800618#ifdef CONFIG_BLK_DEV_RAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int fd;
Eric Piela1e6b6c2009-05-06 16:03:06 -0700620 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 err = unpack_to_rootfs((char *)initrd_start,
Li, Shaohuadf520922008-08-13 17:26:01 +0800622 initrd_end - initrd_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!err) {
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700624 free_initrd();
Tejun Heobb813f42013-01-18 14:05:56 -0800625 goto done;
Li, Shaohuadf520922008-08-13 17:26:01 +0800626 } else {
627 clean_rootfs();
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700628 unpack_to_rootfs(__initramfs_start, __initramfs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Simon Kitchingc1c490e2009-04-02 16:57:00 -0700630 printk(KERN_INFO "rootfs image is not initramfs (%s)"
631 "; looks like an initrd\n", err);
H Hartley Sweetenc67e5382012-05-31 16:26:10 -0700632 fd = sys_open("/initrd.image",
Namhyung Kim562f5e62010-10-26 14:22:42 -0700633 O_WRONLY|O_CREAT, 0700);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (fd >= 0) {
Yinghai Lu38747432014-08-08 14:23:12 -0700635 ssize_t written = xwrite(fd, (char *)initrd_start,
636 initrd_end - initrd_start);
637
638 if (written != initrd_end - initrd_start)
639 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
640 written, initrd_end - initrd_start);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 sys_close(fd);
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700643 free_initrd();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Tejun Heobb813f42013-01-18 14:05:56 -0800645 done:
Linus Torvalds394e4f52017-05-06 10:27:13 -0700646 /* empty statement */;
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800647#else
Eric Piela1e6b6c2009-05-06 16:03:06 -0700648 printk(KERN_INFO "Unpacking initramfs...\n");
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800649 err = unpack_to_rootfs((char *)initrd_start,
Li, Shaohuadf520922008-08-13 17:26:01 +0800650 initrd_end - initrd_start);
Eric Piela1e6b6c2009-05-06 16:03:06 -0700651 if (err)
652 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800653 free_initrd();
654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Stafford Horne17a9be32017-05-04 21:15:56 +0900656 flush_delayed_fput();
657 /*
658 * Try loading default modules from initramfs. This gives
659 * us a chance to load before device_initcalls.
660 */
661 load_default_modules();
662
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800665rootfs_initcall(populate_rootfs);