blob: 8ec1be4d7d5121498e606ef522a7c4eeeef3e52b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/init.h>
3#include <linux/fs.h>
4#include <linux/slab.h>
5#include <linux/types.h>
6#include <linux/fcntl.h>
7#include <linux/delay.h>
8#include <linux/string.h>
Li, Shaohuadf520922008-08-13 17:26:01 +08009#include <linux/dirent.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/syscalls.h>
Nye Liu889d51a2008-10-15 22:01:40 -070011#include <linux/utime.h>
Lokesh Vutla08865512017-02-27 14:28:12 -080012#include <linux/file.h>
Mike Rapoport899ee4a2019-09-28 11:02:26 +030013#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Yinghai Lu38747432014-08-08 14:23:12 -070015static ssize_t __init xwrite(int fd, const char *p, size_t count)
16{
17 ssize_t out = 0;
18
19 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
20 while (count) {
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +010021 ssize_t rv = ksys_write(fd, p, count);
Yinghai Lu38747432014-08-08 14:23:12 -070022
23 if (rv < 0) {
24 if (rv == -EINTR || rv == -EAGAIN)
25 continue;
26 return out ? out : rv;
27 } else if (rv == 0)
28 break;
29
30 p += rv;
31 out += rv;
32 count -= rv;
33 }
34
35 return out;
36}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static __initdata char *message;
39static void __init error(char *x)
40{
41 if (!message)
42 message = x;
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* link hash */
46
Mark Huang6a050da2006-05-15 09:44:03 -070047#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static __initdata struct hash {
50 int ino, minor, major;
Al Viro685dd2d2011-07-26 04:34:13 -040051 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct hash *next;
Mark Huang6a050da2006-05-15 09:44:03 -070053 char name[N_ALIGN(PATH_MAX)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054} *head[32];
55
56static inline int hash(int major, int minor, int ino)
57{
58 unsigned long tmp = ino + minor + (major << 3);
59 tmp += tmp >> 5;
60 return tmp & 31;
61}
62
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070063static char __init *find_link(int major, int minor, int ino,
Al Viro685dd2d2011-07-26 04:34:13 -040064 umode_t mode, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct hash **p, *q;
67 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
68 if ((*p)->ino != ino)
69 continue;
70 if ((*p)->minor != minor)
71 continue;
72 if ((*p)->major != major)
73 continue;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070074 if (((*p)->mode ^ mode) & S_IFMT)
75 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return (*p)->name;
77 }
Thomas Petazzoni3265e662008-04-29 00:59:43 -070078 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (!q)
80 panic("can't allocate link hash entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 q->major = major;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070082 q->minor = minor;
83 q->ino = ino;
84 q->mode = mode;
Mark Huang6a050da2006-05-15 09:44:03 -070085 strcpy(q->name, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 q->next = NULL;
87 *p = q;
88 return NULL;
89}
90
91static void __init free_hash(void)
92{
93 struct hash **p, *q;
94 for (p = head; p < head + 32; p++) {
95 while (*p) {
96 q = *p;
97 *p = q->next;
Thomas Petazzoni3265e662008-04-29 00:59:43 -070098 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 }
101}
102
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800103static long __init do_utime(char *filename, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700104{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700105 struct timespec64 t[2];
Nye Liu889d51a2008-10-15 22:01:40 -0700106
107 t[0].tv_sec = mtime;
108 t[0].tv_nsec = 0;
109 t[1].tv_sec = mtime;
110 t[1].tv_nsec = 0;
111
112 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
113}
114
115static __initdata LIST_HEAD(dir_list);
116struct dir_entry {
117 struct list_head list;
118 char *name;
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800119 time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700120};
121
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800122static void __init dir_add(const char *name, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700123{
124 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
125 if (!de)
126 panic("can't allocate dir_entry buffer");
127 INIT_LIST_HEAD(&de->list);
128 de->name = kstrdup(name, GFP_KERNEL);
129 de->mtime = mtime;
130 list_add(&de->list, &dir_list);
131}
132
133static void __init dir_utime(void)
134{
135 struct dir_entry *de, *tmp;
136 list_for_each_entry_safe(de, tmp, &dir_list, list) {
137 list_del(&de->list);
138 do_utime(de->name, de->mtime);
139 kfree(de->name);
140 kfree(de);
141 }
142}
143
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800144static __initdata time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* cpio header parsing */
147
148static __initdata unsigned long ino, major, minor, nlink;
Al Viro685dd2d2011-07-26 04:34:13 -0400149static __initdata umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static __initdata unsigned long body_len, name_len;
151static __initdata uid_t uid;
152static __initdata gid_t gid;
153static __initdata unsigned rdev;
154
155static void __init parse_header(char *s)
156{
157 unsigned long parsed[12];
158 char buf[9];
159 int i;
160
161 buf[8] = '\0';
162 for (i = 0, s += 6; i < 12; i++, s += 8) {
163 memcpy(buf, s, 8);
164 parsed[i] = simple_strtoul(buf, NULL, 16);
165 }
166 ino = parsed[0];
167 mode = parsed[1];
168 uid = parsed[2];
169 gid = parsed[3];
170 nlink = parsed[4];
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800171 mtime = parsed[5]; /* breaks in y2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 body_len = parsed[6];
173 major = parsed[7];
174 minor = parsed[8];
175 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
176 name_len = parsed[11];
177}
178
179/* FSM */
180
181static __initdata enum state {
182 Start,
183 Collect,
184 GotHeader,
185 SkipIt,
186 GotName,
187 CopyFile,
188 GotSymlink,
189 Reset
190} state, next_state;
191
192static __initdata char *victim;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700193static unsigned long byte_count __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static __initdata loff_t this_header, next_header;
195
Al Virob0a5ab92007-07-26 17:33:59 +0100196static inline void __init eat(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 victim += n;
199 this_header += n;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700200 byte_count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Nye Liu889d51a2008-10-15 22:01:40 -0700203static __initdata char *vcollected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static __initdata char *collected;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700205static long remains __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static __initdata char *collect;
207
208static void __init read_into(char *buf, unsigned size, enum state next)
209{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700210 if (byte_count >= size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 collected = victim;
212 eat(size);
213 state = next;
214 } else {
215 collect = collected = buf;
216 remains = size;
217 next_state = next;
218 state = Collect;
219 }
220}
221
222static __initdata char *header_buf, *symlink_buf, *name_buf;
223
224static int __init do_start(void)
225{
226 read_into(header_buf, 110, GotHeader);
227 return 0;
228}
229
230static int __init do_collect(void)
231{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700232 unsigned long n = remains;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700233 if (byte_count < n)
234 n = byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 memcpy(collect, victim, n);
236 eat(n);
237 collect += n;
238 if ((remains -= n) != 0)
239 return 1;
240 state = next_state;
241 return 0;
242}
243
244static int __init do_header(void)
245{
Arjan van de Ven2e591bb2006-12-06 20:37:19 -0800246 if (memcmp(collected, "070707", 6)==0) {
247 error("incorrect cpio method used: use -H newc option");
248 return 1;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (memcmp(collected, "070701", 6)) {
251 error("no cpio magic");
252 return 1;
253 }
254 parse_header(collected);
255 next_header = this_header + N_ALIGN(name_len) + body_len;
256 next_header = (next_header + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 state = SkipIt;
258 if (name_len <= 0 || name_len > PATH_MAX)
259 return 0;
260 if (S_ISLNK(mode)) {
261 if (body_len > PATH_MAX)
262 return 0;
263 collect = collected = symlink_buf;
264 remains = N_ALIGN(name_len) + body_len;
265 next_state = GotSymlink;
266 state = Collect;
267 return 0;
268 }
269 if (S_ISREG(mode) || !body_len)
270 read_into(name_buf, N_ALIGN(name_len), GotName);
271 return 0;
272}
273
274static int __init do_skip(void)
275{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700276 if (this_header + byte_count < next_header) {
277 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 1;
279 } else {
280 eat(next_header - this_header);
281 state = next_state;
282 return 0;
283 }
284}
285
286static int __init do_reset(void)
287{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700288 while (byte_count && *victim == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 eat(1);
Mark Rustadc34d85a2014-10-13 15:54:07 -0700290 if (byte_count && (this_header & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 error("broken padding");
292 return 1;
293}
294
Mark Rustadc34d85a2014-10-13 15:54:07 -0700295static void __init clean_path(char *path, umode_t fmode)
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700296{
Arnd Bergmann046aa122017-05-08 15:57:00 -0700297 struct kstat st;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700298
Arnd Bergmann046aa122017-05-08 15:57:00 -0700299 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
300 if (S_ISDIR(st.mode))
Dominik Brodowskif459dffa2018-03-11 11:34:48 +0100301 ksys_rmdir(path);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700302 else
Dominik Brodowski0f32ab82018-03-11 11:34:47 +0100303 ksys_unlink(path);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700304 }
305}
306
Li Zhijian7c0950d2018-11-30 14:10:09 -0800307static int __init maybe_link(void)
308{
309 if (nlink >= 2) {
310 char *old = find_link(major, minor, ino, mode, collected);
311 if (old) {
312 clean_path(collected, 0);
313 return (ksys_link(old, collected) < 0) ? -1 : 1;
314 }
315 }
316 return 0;
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static __initdata int wfd;
320
321static int __init do_name(void)
322{
323 state = SkipIt;
324 next_state = Reset;
325 if (strcmp(collected, "TRAILER!!!") == 0) {
326 free_hash();
327 return 0;
328 }
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700329 clean_path(collected, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (S_ISREG(mode)) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700331 int ml = maybe_link();
332 if (ml >= 0) {
333 int openflags = O_WRONLY|O_CREAT;
334 if (ml != 1)
335 openflags |= O_TRUNC;
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100336 wfd = ksys_open(collected, openflags, mode);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (wfd >= 0) {
Dominik Brodowski55731b32018-03-11 11:34:55 +0100339 ksys_fchown(wfd, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100340 ksys_fchmod(wfd, mode);
Randy Robertsond20d5a72009-04-13 14:40:04 -0700341 if (body_len)
Dominik Brodowski411d9472018-03-11 11:34:54 +0100342 ksys_ftruncate(wfd, body_len);
Nye Liu889d51a2008-10-15 22:01:40 -0700343 vcollected = kstrdup(collected, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 state = CopyFile;
345 }
346 }
347 } else if (S_ISDIR(mode)) {
Dominik Brodowski0101db72018-03-11 11:34:49 +0100348 ksys_mkdir(collected, mode);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100349 ksys_chown(collected, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100350 ksys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700351 dir_add(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
353 S_ISFIFO(mode) || S_ISSOCK(mode)) {
354 if (maybe_link() == 0) {
Dominik Brodowski87c4e192018-03-11 11:34:50 +0100355 ksys_mknod(collected, mode, rdev);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100356 ksys_chown(collected, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100357 ksys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700358 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 }
361 return 0;
362}
363
364static int __init do_copy(void)
365{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700366 if (byte_count >= body_len) {
David Engraf9687fd92014-08-08 14:23:16 -0700367 if (xwrite(wfd, victim, body_len) != body_len)
368 error("write error");
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100369 ksys_close(wfd);
Nye Liu889d51a2008-10-15 22:01:40 -0700370 do_utime(vcollected, mtime);
371 kfree(vcollected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 eat(body_len);
373 state = SkipIt;
374 return 0;
375 } else {
Mark Rustadc34d85a2014-10-13 15:54:07 -0700376 if (xwrite(wfd, victim, byte_count) != byte_count)
David Engraf9687fd92014-08-08 14:23:16 -0700377 error("write error");
Mark Rustadc34d85a2014-10-13 15:54:07 -0700378 body_len -= byte_count;
379 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 1;
381 }
382}
383
384static int __init do_symlink(void)
385{
386 collected[N_ALIGN(name_len) + body_len] = '\0';
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700387 clean_path(collected, 0);
Dominik Brodowskib724e842018-03-11 11:34:49 +0100388 ksys_symlink(collected + N_ALIGN(name_len), collected);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100389 ksys_lchown(collected, uid, gid);
Nye Liu889d51a2008-10-15 22:01:40 -0700390 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 state = SkipIt;
392 next_state = Reset;
393 return 0;
394}
395
396static __initdata int (*actions[])(void) = {
397 [Start] = do_start,
398 [Collect] = do_collect,
399 [GotHeader] = do_header,
400 [SkipIt] = do_skip,
401 [GotName] = do_name,
402 [CopyFile] = do_copy,
403 [GotSymlink] = do_symlink,
404 [Reset] = do_reset,
405};
406
Yinghai Lud97b07c2014-08-08 14:23:14 -0700407static long __init write_buffer(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700409 byte_count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 victim = buf;
411
412 while (!actions[state]())
413 ;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700414 return len - byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Yinghai Lud97b07c2014-08-08 14:23:14 -0700417static long __init flush_buffer(void *bufv, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Alain Knaff30d65db2009-01-04 22:46:17 +0100419 char *buf = (char *) bufv;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700420 long written;
421 long origLen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (message)
Alain Knaff30d65db2009-01-04 22:46:17 +0100423 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 while ((written = write_buffer(buf, len)) < len && !message) {
425 char c = buf[written];
426 if (c == '0') {
427 buf += written;
428 len -= written;
429 state = Start;
430 } else if (c == 0) {
431 buf += written;
432 len -= written;
433 state = Reset;
434 } else
David Engrafe5eed352019-03-07 16:30:19 -0800435 error("junk within compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Alain Knaff30d65db2009-01-04 22:46:17 +0100437 return origLen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Yinghai Lud97b07c2014-08-08 14:23:14 -0700440static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800442#include <linux/decompress/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Yinghai Lud97b07c2014-08-08 14:23:14 -0700444static char * __init unpack_to_rootfs(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700446 long written;
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800447 decompress_fn decompress;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800448 const char *compress_name;
449 static __initdata char msg_buf[64];
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800450
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700451 header_buf = kmalloc(110, GFP_KERNEL);
452 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
453 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
Alain Knaff30d65db2009-01-04 22:46:17 +0100454
455 if (!header_buf || !symlink_buf || !name_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 panic("can't allocate buffers");
Alain Knaff30d65db2009-01-04 22:46:17 +0100457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 state = Start;
459 this_header = 0;
460 message = NULL;
461 while (!message && len) {
462 loff_t saved_offset = this_header;
463 if (*buf == '0' && !(this_header & 3)) {
464 state = Start;
465 written = write_buffer(buf, len);
466 buf += written;
467 len -= written;
468 continue;
469 }
470 if (!*buf) {
471 buf++;
472 len--;
473 this_header++;
474 continue;
475 }
476 this_header = 0;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800477 decompress = decompress_method(buf, len, &compress_name);
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -0700478 pr_debug("Detected %s compressed data\n", compress_name);
Phillip Lougher54291362009-12-14 21:45:19 +0000479 if (decompress) {
Yinghai Lud97b07c2014-08-08 14:23:14 -0700480 int res = decompress(buf, len, NULL, flush_buffer, NULL,
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800481 &my_inptr, error);
Phillip Lougher54291362009-12-14 21:45:19 +0000482 if (res)
483 error("decompressor failed");
484 } else if (compress_name) {
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800485 if (!message) {
486 snprintf(msg_buf, sizeof msg_buf,
487 "compression method %s not configured",
488 compress_name);
489 message = msg_buf;
490 }
Phillip Lougherdf37bd12010-04-23 13:18:11 -0400491 } else
David Engrafe5eed352019-03-07 16:30:19 -0800492 error("invalid magic at start of compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (state != Reset)
David Engrafe5eed352019-03-07 16:30:19 -0800494 error("junk at the end of compressed archive");
Alain Knaff30d65db2009-01-04 22:46:17 +0100495 this_header = saved_offset + my_inptr;
496 buf += my_inptr;
497 len -= my_inptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Nye Liu889d51a2008-10-15 22:01:40 -0700499 dir_utime();
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700500 kfree(name_buf);
501 kfree(symlink_buf);
502 kfree(header_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return message;
504}
505
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800506static int __initdata do_retain_initrd;
507
508static int __init retain_initrd_param(char *str)
509{
510 if (*str)
511 return 0;
512 do_retain_initrd = 1;
513 return 1;
514}
515__setup("retain_initrd", retain_initrd_param);
516
Christoph Hellwigd8ae8a32019-05-13 17:18:30 -0700517#ifdef CONFIG_ARCH_HAS_KEEPINITRD
518static int __init keepinitrd_setup(char *__unused)
519{
520 do_retain_initrd = 1;
521 return 1;
522}
523__setup("keepinitrd", keepinitrd_setup);
524#endif
525
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700526extern char __initramfs_start[];
527extern unsigned long __initramfs_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#include <linux/initrd.h>
Haren Myneni9c15e852006-02-10 01:51:05 -0800529#include <linux/kexec.h>
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700530
Christoph Hellwig4afd58e2019-05-13 17:18:34 -0700531void __weak free_initrd_mem(unsigned long start, unsigned long end)
532{
Mike Rapoport899ee4a2019-09-28 11:02:26 +0300533#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
534 unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
535 unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
536
537 memblock_free(__pa(aligned_start), aligned_end - aligned_start);
538#endif
539
Christoph Hellwigf94f7432019-05-13 17:18:37 -0700540 free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
541 "initrd");
Christoph Hellwig4afd58e2019-05-13 17:18:34 -0700542}
543
Dave Young2965faa2015-09-09 15:38:55 -0700544#ifdef CONFIG_KEXEC_CORE
Christoph Hellwig23091e22019-05-13 17:18:21 -0700545static bool kexec_free_initrd(void)
546{
Haren Myneni9c15e852006-02-10 01:51:05 -0800547 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
548 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
549
550 /*
551 * If the initrd region is overlapped with crashkernel reserved region,
552 * free only memory that is not part of crashkernel region.
553 */
Christoph Hellwig23091e22019-05-13 17:18:21 -0700554 if (initrd_start >= crashk_end || initrd_end <= crashk_start)
555 return false;
556
557 /*
558 * Initialize initrd memory region since the kexec boot does not do.
559 */
560 memset((void *)initrd_start, 0, initrd_end - initrd_start);
561 if (initrd_start < crashk_start)
562 free_initrd_mem(initrd_start, crashk_start);
563 if (initrd_end > crashk_end)
564 free_initrd_mem(crashk_end, initrd_end);
565 return true;
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700566}
Christoph Hellwig23091e22019-05-13 17:18:21 -0700567#else
568static inline bool kexec_free_initrd(void)
569{
570 return false;
571}
572#endif /* CONFIG_KEXEC_CORE */
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700573
Andrew Mortona841c672019-02-20 22:18:52 -0800574#ifdef CONFIG_BLK_DEV_RAM
Li, Shaohuadf520922008-08-13 17:26:01 +0800575#define BUF_SIZE 1024
576static void __init clean_rootfs(void)
577{
578 int fd;
579 void *buf;
580 struct linux_dirent64 *dirp;
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800581 int num;
Li, Shaohuadf520922008-08-13 17:26:01 +0800582
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100583 fd = ksys_open("/", O_RDONLY, 0);
Li, Shaohuadf520922008-08-13 17:26:01 +0800584 WARN_ON(fd < 0);
585 if (fd < 0)
586 return;
587 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
588 WARN_ON(!buf);
589 if (!buf) {
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100590 ksys_close(fd);
Li, Shaohuadf520922008-08-13 17:26:01 +0800591 return;
592 }
593
594 dirp = buf;
Dominik Brodowski454dab32018-03-13 21:34:04 +0100595 num = ksys_getdents64(fd, dirp, BUF_SIZE);
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800596 while (num > 0) {
597 while (num > 0) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700598 struct kstat st;
Li, Shaohuadf520922008-08-13 17:26:01 +0800599 int ret;
600
Arnd Bergmann046aa122017-05-08 15:57:00 -0700601 ret = vfs_lstat(dirp->d_name, &st);
Li, Shaohuadf520922008-08-13 17:26:01 +0800602 WARN_ON_ONCE(ret);
603 if (!ret) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700604 if (S_ISDIR(st.mode))
Dominik Brodowskif459dffa2018-03-11 11:34:48 +0100605 ksys_rmdir(dirp->d_name);
Li, Shaohuadf520922008-08-13 17:26:01 +0800606 else
Dominik Brodowski0f32ab82018-03-11 11:34:47 +0100607 ksys_unlink(dirp->d_name);
Li, Shaohuadf520922008-08-13 17:26:01 +0800608 }
609
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800610 num -= dirp->d_reclen;
Li, Shaohuadf520922008-08-13 17:26:01 +0800611 dirp = (void *)dirp + dirp->d_reclen;
612 }
613 dirp = buf;
614 memset(buf, 0, BUF_SIZE);
Dominik Brodowski454dab32018-03-13 21:34:04 +0100615 num = ksys_getdents64(fd, dirp, BUF_SIZE);
Li, Shaohuadf520922008-08-13 17:26:01 +0800616 }
617
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100618 ksys_close(fd);
Li, Shaohuadf520922008-08-13 17:26:01 +0800619 kfree(buf);
620}
Christoph Hellwigafef7882019-05-13 17:18:27 -0700621#else
622static inline void clean_rootfs(void)
623{
624}
625#endif /* CONFIG_BLK_DEV_RAM */
Li, Shaohuadf520922008-08-13 17:26:01 +0800626
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700627#ifdef CONFIG_BLK_DEV_RAM
Geert Uytterhoeven4ada1e82019-06-28 12:07:03 -0700628static void __init populate_initrd_image(char *err)
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700629{
630 ssize_t written;
631 int fd;
632
633 unpack_to_rootfs(__initramfs_start, __initramfs_size);
634
635 printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
636 err);
637 fd = ksys_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
638 if (fd < 0)
639 return;
640
641 written = xwrite(fd, (char *)initrd_start, initrd_end - initrd_start);
642 if (written != initrd_end - initrd_start)
643 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
644 written, initrd_end - initrd_start);
645 ksys_close(fd);
646}
Christoph Hellwigafef7882019-05-13 17:18:27 -0700647#else
Geert Uytterhoeven4ada1e82019-06-28 12:07:03 -0700648static void __init populate_initrd_image(char *err)
Christoph Hellwigafef7882019-05-13 17:18:27 -0700649{
650 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
651}
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700652#endif /* CONFIG_BLK_DEV_RAM */
653
Linus Torvalds9a9e0d62008-03-15 11:53:32 -0700654static int __init populate_rootfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Stafford Horne17a9be32017-05-04 21:15:56 +0900656 /* Load the built in initramfs */
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700657 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (err)
Tetsuo Handa499a4582014-01-23 15:54:56 -0800659 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
Christoph Hellwig54c7a892019-05-13 17:18:17 -0700660
Christoph Hellwigafef7882019-05-13 17:18:27 -0700661 if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
662 goto done;
663
664 if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
665 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
666 else
667 printk(KERN_INFO "Unpacking initramfs...\n");
668
669 err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
670 if (err) {
Christoph Hellwig54c7a892019-05-13 17:18:17 -0700671 clean_rootfs();
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700672 populate_initrd_image(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Christoph Hellwig23091e22019-05-13 17:18:21 -0700674
Christoph Hellwigafef7882019-05-13 17:18:27 -0700675done:
Christoph Hellwig23091e22019-05-13 17:18:21 -0700676 /*
677 * If the initrd region is overlapped with crashkernel reserved region,
678 * free only memory that is not part of crashkernel region.
679 */
Steven Price5d59aa82019-05-17 14:31:47 -0700680 if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
Christoph Hellwig23091e22019-05-13 17:18:21 -0700681 free_initrd_mem(initrd_start, initrd_end);
682 initrd_start = 0;
683 initrd_end = 0;
684
Stafford Horne17a9be32017-05-04 21:15:56 +0900685 flush_delayed_fput();
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800688rootfs_initcall(populate_rootfs);