blob: 9820fca4d4e36ced878d8a2a4827e888aff34385 [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>
Christoph Hellwigb2a74d52020-06-06 13:59:54 +020014#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Christoph Hellwigbf6419e2020-07-14 08:56:19 +020016static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
17 loff_t *pos)
Yinghai Lu38747432014-08-08 14:23:12 -070018{
19 ssize_t out = 0;
20
21 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
22 while (count) {
Christoph Hellwigbf6419e2020-07-14 08:56:19 +020023 ssize_t rv = kernel_write(file, p, count, pos);
Yinghai Lu38747432014-08-08 14:23:12 -070024
25 if (rv < 0) {
26 if (rv == -EINTR || rv == -EAGAIN)
27 continue;
28 return out ? out : rv;
29 } else if (rv == 0)
30 break;
31
32 p += rv;
33 out += rv;
34 count -= rv;
35 }
36
37 return out;
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static __initdata char *message;
41static void __init error(char *x)
42{
43 if (!message)
44 message = x;
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* link hash */
48
Mark Huang6a050da2006-05-15 09:44:03 -070049#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static __initdata struct hash {
52 int ino, minor, major;
Al Viro685dd2d2011-07-26 04:34:13 -040053 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct hash *next;
Mark Huang6a050da2006-05-15 09:44:03 -070055 char name[N_ALIGN(PATH_MAX)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056} *head[32];
57
58static inline int hash(int major, int minor, int ino)
59{
60 unsigned long tmp = ino + minor + (major << 3);
61 tmp += tmp >> 5;
62 return tmp & 31;
63}
64
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070065static char __init *find_link(int major, int minor, int ino,
Al Viro685dd2d2011-07-26 04:34:13 -040066 umode_t mode, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct hash **p, *q;
69 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
70 if ((*p)->ino != ino)
71 continue;
72 if ((*p)->minor != minor)
73 continue;
74 if ((*p)->major != major)
75 continue;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070076 if (((*p)->mode ^ mode) & S_IFMT)
77 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return (*p)->name;
79 }
Thomas Petazzoni3265e662008-04-29 00:59:43 -070080 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (!q)
82 panic("can't allocate link hash entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 q->major = major;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070084 q->minor = minor;
85 q->ino = ino;
86 q->mode = mode;
Mark Huang6a050da2006-05-15 09:44:03 -070087 strcpy(q->name, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 q->next = NULL;
89 *p = q;
90 return NULL;
91}
92
93static void __init free_hash(void)
94{
95 struct hash **p, *q;
96 for (p = head; p < head + 32; p++) {
97 while (*p) {
98 q = *p;
99 *p = q->next;
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700100 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 }
103}
104
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800105static long __init do_utime(char *filename, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700106{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700107 struct timespec64 t[2];
Nye Liu889d51a2008-10-15 22:01:40 -0700108
109 t[0].tv_sec = mtime;
110 t[0].tv_nsec = 0;
111 t[1].tv_sec = mtime;
112 t[1].tv_nsec = 0;
113
114 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
115}
116
117static __initdata LIST_HEAD(dir_list);
118struct dir_entry {
119 struct list_head list;
120 char *name;
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800121 time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700122};
123
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800124static void __init dir_add(const char *name, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700125{
126 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
127 if (!de)
128 panic("can't allocate dir_entry buffer");
129 INIT_LIST_HEAD(&de->list);
130 de->name = kstrdup(name, GFP_KERNEL);
131 de->mtime = mtime;
132 list_add(&de->list, &dir_list);
133}
134
135static void __init dir_utime(void)
136{
137 struct dir_entry *de, *tmp;
138 list_for_each_entry_safe(de, tmp, &dir_list, list) {
139 list_del(&de->list);
140 do_utime(de->name, de->mtime);
141 kfree(de->name);
142 kfree(de);
143 }
144}
145
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800146static __initdata time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* cpio header parsing */
149
150static __initdata unsigned long ino, major, minor, nlink;
Al Viro685dd2d2011-07-26 04:34:13 -0400151static __initdata umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static __initdata unsigned long body_len, name_len;
153static __initdata uid_t uid;
154static __initdata gid_t gid;
155static __initdata unsigned rdev;
156
157static void __init parse_header(char *s)
158{
159 unsigned long parsed[12];
160 char buf[9];
161 int i;
162
163 buf[8] = '\0';
164 for (i = 0, s += 6; i < 12; i++, s += 8) {
165 memcpy(buf, s, 8);
166 parsed[i] = simple_strtoul(buf, NULL, 16);
167 }
168 ino = parsed[0];
169 mode = parsed[1];
170 uid = parsed[2];
171 gid = parsed[3];
172 nlink = parsed[4];
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800173 mtime = parsed[5]; /* breaks in y2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 body_len = parsed[6];
175 major = parsed[7];
176 minor = parsed[8];
177 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
178 name_len = parsed[11];
179}
180
181/* FSM */
182
183static __initdata enum state {
184 Start,
185 Collect,
186 GotHeader,
187 SkipIt,
188 GotName,
189 CopyFile,
190 GotSymlink,
191 Reset
192} state, next_state;
193
194static __initdata char *victim;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700195static unsigned long byte_count __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static __initdata loff_t this_header, next_header;
197
Al Virob0a5ab92007-07-26 17:33:59 +0100198static inline void __init eat(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 victim += n;
201 this_header += n;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700202 byte_count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static __initdata char *collected;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700206static long remains __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static __initdata char *collect;
208
209static void __init read_into(char *buf, unsigned size, enum state next)
210{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700211 if (byte_count >= size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 collected = victim;
213 eat(size);
214 state = next;
215 } else {
216 collect = collected = buf;
217 remains = size;
218 next_state = next;
219 state = Collect;
220 }
221}
222
223static __initdata char *header_buf, *symlink_buf, *name_buf;
224
225static int __init do_start(void)
226{
227 read_into(header_buf, 110, GotHeader);
228 return 0;
229}
230
231static int __init do_collect(void)
232{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700233 unsigned long n = remains;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700234 if (byte_count < n)
235 n = byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 memcpy(collect, victim, n);
237 eat(n);
238 collect += n;
239 if ((remains -= n) != 0)
240 return 1;
241 state = next_state;
242 return 0;
243}
244
245static int __init do_header(void)
246{
Arjan van de Ven2e591bb2006-12-06 20:37:19 -0800247 if (memcmp(collected, "070707", 6)==0) {
248 error("incorrect cpio method used: use -H newc option");
249 return 1;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (memcmp(collected, "070701", 6)) {
252 error("no cpio magic");
253 return 1;
254 }
255 parse_header(collected);
256 next_header = this_header + N_ALIGN(name_len) + body_len;
257 next_header = (next_header + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 state = SkipIt;
259 if (name_len <= 0 || name_len > PATH_MAX)
260 return 0;
261 if (S_ISLNK(mode)) {
262 if (body_len > PATH_MAX)
263 return 0;
264 collect = collected = symlink_buf;
265 remains = N_ALIGN(name_len) + body_len;
266 next_state = GotSymlink;
267 state = Collect;
268 return 0;
269 }
270 if (S_ISREG(mode) || !body_len)
271 read_into(name_buf, N_ALIGN(name_len), GotName);
272 return 0;
273}
274
275static int __init do_skip(void)
276{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700277 if (this_header + byte_count < next_header) {
278 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 1;
280 } else {
281 eat(next_header - this_header);
282 state = next_state;
283 return 0;
284 }
285}
286
287static int __init do_reset(void)
288{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700289 while (byte_count && *victim == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 eat(1);
Mark Rustadc34d85a2014-10-13 15:54:07 -0700291 if (byte_count && (this_header & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 error("broken padding");
293 return 1;
294}
295
Mark Rustadc34d85a2014-10-13 15:54:07 -0700296static void __init clean_path(char *path, umode_t fmode)
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700297{
Arnd Bergmann046aa122017-05-08 15:57:00 -0700298 struct kstat st;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700299
Arnd Bergmann046aa122017-05-08 15:57:00 -0700300 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
301 if (S_ISDIR(st.mode))
Dominik Brodowskif459dffa2018-03-11 11:34:48 +0100302 ksys_rmdir(path);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700303 else
Dominik Brodowski0f32ab82018-03-11 11:34:47 +0100304 ksys_unlink(path);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700305 }
306}
307
Li Zhijian7c0950d2018-11-30 14:10:09 -0800308static int __init maybe_link(void)
309{
310 if (nlink >= 2) {
311 char *old = find_link(major, minor, ino, mode, collected);
312 if (old) {
313 clean_path(collected, 0);
314 return (ksys_link(old, collected) < 0) ? -1 : 1;
315 }
316 }
317 return 0;
318}
319
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200320static __initdata struct file *wfile;
321static __initdata loff_t wfile_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323static int __init do_name(void)
324{
325 state = SkipIt;
326 next_state = Reset;
327 if (strcmp(collected, "TRAILER!!!") == 0) {
328 free_hash();
329 return 0;
330 }
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700331 clean_path(collected, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (S_ISREG(mode)) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700333 int ml = maybe_link();
334 if (ml >= 0) {
335 int openflags = O_WRONLY|O_CREAT;
336 if (ml != 1)
337 openflags |= O_TRUNC;
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200338 wfile = filp_open(collected, openflags, mode);
339 if (IS_ERR(wfile))
340 return 0;
341 wfile_pos = 0;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700342
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200343 vfs_fchown(wfile, uid, gid);
344 vfs_fchmod(wfile, mode);
345 if (body_len)
346 vfs_truncate(&wfile->f_path, body_len);
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200347 state = CopyFile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 } else if (S_ISDIR(mode)) {
Dominik Brodowski0101db72018-03-11 11:34:49 +0100350 ksys_mkdir(collected, mode);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100351 ksys_chown(collected, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100352 ksys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700353 dir_add(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
355 S_ISFIFO(mode) || S_ISSOCK(mode)) {
356 if (maybe_link() == 0) {
Dominik Brodowski87c4e192018-03-11 11:34:50 +0100357 ksys_mknod(collected, mode, rdev);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100358 ksys_chown(collected, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100359 ksys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700360 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 }
363 return 0;
364}
365
366static int __init do_copy(void)
367{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700368 if (byte_count >= body_len) {
Christoph Hellwig38b08222020-07-30 08:19:00 +0200369 struct timespec64 t[2] = { };
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200370 if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
David Engraf9687fd92014-08-08 14:23:16 -0700371 error("write error");
Christoph Hellwig38b08222020-07-30 08:19:00 +0200372
373 t[0].tv_sec = mtime;
374 t[1].tv_sec = mtime;
375 vfs_utimes(&wfile->f_path, t);
376
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200377 fput(wfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 eat(body_len);
379 state = SkipIt;
380 return 0;
381 } else {
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200382 if (xwrite(wfile, victim, byte_count, &wfile_pos) != 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);
Dominik Brodowskib724e842018-03-11 11:34:49 +0100394 ksys_symlink(collected + N_ALIGN(name_len), collected);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100395 ksys_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
David Engrafe5eed352019-03-07 16:30:19 -0800441 error("junk within compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
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
David Engrafe5eed352019-03-07 16:30:19 -0800498 error("invalid magic at start of compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (state != Reset)
David Engrafe5eed352019-03-07 16:30:19 -0800500 error("junk at the end of compressed archive");
Alain Knaff30d65db2009-01-04 22:46:17 +0100501 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
Christoph Hellwigd8ae8a32019-05-13 17:18:30 -0700523#ifdef CONFIG_ARCH_HAS_KEEPINITRD
524static int __init keepinitrd_setup(char *__unused)
525{
526 do_retain_initrd = 1;
527 return 1;
528}
529__setup("keepinitrd", keepinitrd_setup);
530#endif
531
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700532extern char __initramfs_start[];
533extern unsigned long __initramfs_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534#include <linux/initrd.h>
Haren Myneni9c15e852006-02-10 01:51:05 -0800535#include <linux/kexec.h>
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700536
Christoph Hellwig4afd58e2019-05-13 17:18:34 -0700537void __weak free_initrd_mem(unsigned long start, unsigned long end)
538{
Mike Rapoport899ee4a2019-09-28 11:02:26 +0300539#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
540 unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
541 unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
542
543 memblock_free(__pa(aligned_start), aligned_end - aligned_start);
544#endif
545
Christoph Hellwigf94f7432019-05-13 17:18:37 -0700546 free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
547 "initrd");
Christoph Hellwig4afd58e2019-05-13 17:18:34 -0700548}
549
Dave Young2965faa2015-09-09 15:38:55 -0700550#ifdef CONFIG_KEXEC_CORE
Linus Torvaldse99332e2020-05-09 17:50:03 -0700551static bool __init kexec_free_initrd(void)
Christoph Hellwig23091e22019-05-13 17:18:21 -0700552{
Haren Myneni9c15e852006-02-10 01:51:05 -0800553 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
554 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
555
556 /*
557 * If the initrd region is overlapped with crashkernel reserved region,
558 * free only memory that is not part of crashkernel region.
559 */
Christoph Hellwig23091e22019-05-13 17:18:21 -0700560 if (initrd_start >= crashk_end || initrd_end <= crashk_start)
561 return false;
562
563 /*
564 * Initialize initrd memory region since the kexec boot does not do.
565 */
566 memset((void *)initrd_start, 0, initrd_end - initrd_start);
567 if (initrd_start < crashk_start)
568 free_initrd_mem(initrd_start, crashk_start);
569 if (initrd_end > crashk_end)
570 free_initrd_mem(crashk_end, initrd_end);
571 return true;
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700572}
Christoph Hellwig23091e22019-05-13 17:18:21 -0700573#else
574static inline bool kexec_free_initrd(void)
575{
576 return false;
577}
578#endif /* CONFIG_KEXEC_CORE */
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700579
Andrew Mortona841c672019-02-20 22:18:52 -0800580#ifdef CONFIG_BLK_DEV_RAM
Geert Uytterhoeven4ada1e82019-06-28 12:07:03 -0700581static void __init populate_initrd_image(char *err)
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700582{
583 ssize_t written;
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200584 struct file *file;
585 loff_t pos = 0;
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700586
587 unpack_to_rootfs(__initramfs_start, __initramfs_size);
588
589 printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
590 err);
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200591 file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
592 if (IS_ERR(file))
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700593 return;
594
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200595 written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
596 &pos);
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700597 if (written != initrd_end - initrd_start)
598 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
599 written, initrd_end - initrd_start);
Christoph Hellwigbf6419e2020-07-14 08:56:19 +0200600 fput(file);
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700601}
602#endif /* CONFIG_BLK_DEV_RAM */
603
Linus Torvalds9a9e0d62008-03-15 11:53:32 -0700604static int __init populate_rootfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Stafford Horne17a9be32017-05-04 21:15:56 +0900606 /* Load the built in initramfs */
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700607 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (err)
Tetsuo Handa499a4582014-01-23 15:54:56 -0800609 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
Christoph Hellwig54c7a892019-05-13 17:18:17 -0700610
Christoph Hellwigafef7882019-05-13 17:18:27 -0700611 if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
612 goto done;
613
614 if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
615 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
616 else
617 printk(KERN_INFO "Unpacking initramfs...\n");
618
619 err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
620 if (err) {
Christoph Hellwig9ab6b712020-06-06 13:51:52 +0200621#ifdef CONFIG_BLK_DEV_RAM
Christoph Hellwig7c184ec2019-05-13 17:18:24 -0700622 populate_initrd_image(err);
Christoph Hellwig9ab6b712020-06-06 13:51:52 +0200623#else
624 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Christoph Hellwig23091e22019-05-13 17:18:21 -0700627
Christoph Hellwigafef7882019-05-13 17:18:27 -0700628done:
Christoph Hellwig23091e22019-05-13 17:18:21 -0700629 /*
630 * If the initrd region is overlapped with crashkernel reserved region,
631 * free only memory that is not part of crashkernel region.
632 */
Steven Price5d59aa82019-05-17 14:31:47 -0700633 if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
Christoph Hellwig23091e22019-05-13 17:18:21 -0700634 free_initrd_mem(initrd_start, initrd_end);
635 initrd_start = 0;
636 initrd_end = 0;
637
Stafford Horne17a9be32017-05-04 21:15:56 +0900638 flush_delayed_fput();
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800641rootfs_initcall(populate_rootfs);