blob: 5fda9557a134d98de35b42b84d9ddcad7cb61fc3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Yinghai Lu38747432014-08-08 14:23:12 -070014static ssize_t __init xwrite(int fd, const char *p, size_t count)
15{
16 ssize_t out = 0;
17
18 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
19 while (count) {
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +010020 ssize_t rv = ksys_write(fd, p, count);
Yinghai Lu38747432014-08-08 14:23:12 -070021
22 if (rv < 0) {
23 if (rv == -EINTR || rv == -EAGAIN)
24 continue;
25 return out ? out : rv;
26 } else if (rv == 0)
27 break;
28
29 p += rv;
30 out += rv;
31 count -= rv;
32 }
33
34 return out;
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static __initdata char *message;
38static void __init error(char *x)
39{
40 if (!message)
41 message = x;
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* link hash */
45
Mark Huang6a050da2006-05-15 09:44:03 -070046#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static __initdata struct hash {
49 int ino, minor, major;
Al Viro685dd2d2011-07-26 04:34:13 -040050 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct hash *next;
Mark Huang6a050da2006-05-15 09:44:03 -070052 char name[N_ALIGN(PATH_MAX)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053} *head[32];
54
55static inline int hash(int major, int minor, int ino)
56{
57 unsigned long tmp = ino + minor + (major << 3);
58 tmp += tmp >> 5;
59 return tmp & 31;
60}
61
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070062static char __init *find_link(int major, int minor, int ino,
Al Viro685dd2d2011-07-26 04:34:13 -040063 umode_t mode, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct hash **p, *q;
66 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
67 if ((*p)->ino != ino)
68 continue;
69 if ((*p)->minor != minor)
70 continue;
71 if ((*p)->major != major)
72 continue;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070073 if (((*p)->mode ^ mode) & S_IFMT)
74 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return (*p)->name;
76 }
Thomas Petazzoni3265e662008-04-29 00:59:43 -070077 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!q)
79 panic("can't allocate link hash entry");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 q->major = major;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -070081 q->minor = minor;
82 q->ino = ino;
83 q->mode = mode;
Mark Huang6a050da2006-05-15 09:44:03 -070084 strcpy(q->name, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 q->next = NULL;
86 *p = q;
87 return NULL;
88}
89
90static void __init free_hash(void)
91{
92 struct hash **p, *q;
93 for (p = head; p < head + 32; p++) {
94 while (*p) {
95 q = *p;
96 *p = q->next;
Thomas Petazzoni3265e662008-04-29 00:59:43 -070097 kfree(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 }
100}
101
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800102static long __init do_utime(char *filename, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700103{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700104 struct timespec64 t[2];
Nye Liu889d51a2008-10-15 22:01:40 -0700105
106 t[0].tv_sec = mtime;
107 t[0].tv_nsec = 0;
108 t[1].tv_sec = mtime;
109 t[1].tv_nsec = 0;
110
111 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
112}
113
114static __initdata LIST_HEAD(dir_list);
115struct dir_entry {
116 struct list_head list;
117 char *name;
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800118 time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700119};
120
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800121static void __init dir_add(const char *name, time64_t mtime)
Nye Liu889d51a2008-10-15 22:01:40 -0700122{
123 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
124 if (!de)
125 panic("can't allocate dir_entry buffer");
126 INIT_LIST_HEAD(&de->list);
127 de->name = kstrdup(name, GFP_KERNEL);
128 de->mtime = mtime;
129 list_add(&de->list, &dir_list);
130}
131
132static void __init dir_utime(void)
133{
134 struct dir_entry *de, *tmp;
135 list_for_each_entry_safe(de, tmp, &dir_list, list) {
136 list_del(&de->list);
137 do_utime(de->name, de->mtime);
138 kfree(de->name);
139 kfree(de);
140 }
141}
142
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800143static __initdata time64_t mtime;
Nye Liu889d51a2008-10-15 22:01:40 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* cpio header parsing */
146
147static __initdata unsigned long ino, major, minor, nlink;
Al Viro685dd2d2011-07-26 04:34:13 -0400148static __initdata umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static __initdata unsigned long body_len, name_len;
150static __initdata uid_t uid;
151static __initdata gid_t gid;
152static __initdata unsigned rdev;
153
154static void __init parse_header(char *s)
155{
156 unsigned long parsed[12];
157 char buf[9];
158 int i;
159
160 buf[8] = '\0';
161 for (i = 0, s += 6; i < 12; i++, s += 8) {
162 memcpy(buf, s, 8);
163 parsed[i] = simple_strtoul(buf, NULL, 16);
164 }
165 ino = parsed[0];
166 mode = parsed[1];
167 uid = parsed[2];
168 gid = parsed[3];
169 nlink = parsed[4];
Arnd Bergmanne35c4c62017-11-17 15:31:04 -0800170 mtime = parsed[5]; /* breaks in y2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 body_len = parsed[6];
172 major = parsed[7];
173 minor = parsed[8];
174 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
175 name_len = parsed[11];
176}
177
178/* FSM */
179
180static __initdata enum state {
181 Start,
182 Collect,
183 GotHeader,
184 SkipIt,
185 GotName,
186 CopyFile,
187 GotSymlink,
188 Reset
189} state, next_state;
190
191static __initdata char *victim;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700192static unsigned long byte_count __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static __initdata loff_t this_header, next_header;
194
Al Virob0a5ab92007-07-26 17:33:59 +0100195static inline void __init eat(unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 victim += n;
198 this_header += n;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700199 byte_count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Nye Liu889d51a2008-10-15 22:01:40 -0700202static __initdata char *vcollected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static __initdata char *collected;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700204static long remains __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static __initdata char *collect;
206
207static void __init read_into(char *buf, unsigned size, enum state next)
208{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700209 if (byte_count >= size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 collected = victim;
211 eat(size);
212 state = next;
213 } else {
214 collect = collected = buf;
215 remains = size;
216 next_state = next;
217 state = Collect;
218 }
219}
220
221static __initdata char *header_buf, *symlink_buf, *name_buf;
222
223static int __init do_start(void)
224{
225 read_into(header_buf, 110, GotHeader);
226 return 0;
227}
228
229static int __init do_collect(void)
230{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700231 unsigned long n = remains;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700232 if (byte_count < n)
233 n = byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 memcpy(collect, victim, n);
235 eat(n);
236 collect += n;
237 if ((remains -= n) != 0)
238 return 1;
239 state = next_state;
240 return 0;
241}
242
243static int __init do_header(void)
244{
Arjan van de Ven2e591bb2006-12-06 20:37:19 -0800245 if (memcmp(collected, "070707", 6)==0) {
246 error("incorrect cpio method used: use -H newc option");
247 return 1;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (memcmp(collected, "070701", 6)) {
250 error("no cpio magic");
251 return 1;
252 }
253 parse_header(collected);
254 next_header = this_header + N_ALIGN(name_len) + body_len;
255 next_header = (next_header + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 state = SkipIt;
257 if (name_len <= 0 || name_len > PATH_MAX)
258 return 0;
259 if (S_ISLNK(mode)) {
260 if (body_len > PATH_MAX)
261 return 0;
262 collect = collected = symlink_buf;
263 remains = N_ALIGN(name_len) + body_len;
264 next_state = GotSymlink;
265 state = Collect;
266 return 0;
267 }
268 if (S_ISREG(mode) || !body_len)
269 read_into(name_buf, N_ALIGN(name_len), GotName);
270 return 0;
271}
272
273static int __init do_skip(void)
274{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700275 if (this_header + byte_count < next_header) {
276 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 1;
278 } else {
279 eat(next_header - this_header);
280 state = next_state;
281 return 0;
282 }
283}
284
285static int __init do_reset(void)
286{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700287 while (byte_count && *victim == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 eat(1);
Mark Rustadc34d85a2014-10-13 15:54:07 -0700289 if (byte_count && (this_header & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 error("broken padding");
291 return 1;
292}
293
Mark Rustadc34d85a2014-10-13 15:54:07 -0700294static void __init clean_path(char *path, umode_t fmode)
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700295{
Arnd Bergmann046aa122017-05-08 15:57:00 -0700296 struct kstat st;
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700297
Arnd Bergmann046aa122017-05-08 15:57:00 -0700298 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
299 if (S_ISDIR(st.mode))
Dominik Brodowskif459dffa2018-03-11 11:34:48 +0100300 ksys_rmdir(path);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700301 else
Dominik Brodowski0f32ab82018-03-11 11:34:47 +0100302 ksys_unlink(path);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700303 }
304}
305
Li Zhijian7c0950d2018-11-30 14:10:09 -0800306static int __init maybe_link(void)
307{
308 if (nlink >= 2) {
309 char *old = find_link(major, minor, ino, mode, collected);
310 if (old) {
311 clean_path(collected, 0);
312 return (ksys_link(old, collected) < 0) ? -1 : 1;
313 }
314 }
315 return 0;
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static __initdata int wfd;
319
320static int __init do_name(void)
321{
322 state = SkipIt;
323 next_state = Reset;
324 if (strcmp(collected, "TRAILER!!!") == 0) {
325 free_hash();
326 return 0;
327 }
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700328 clean_path(collected, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (S_ISREG(mode)) {
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700330 int ml = maybe_link();
331 if (ml >= 0) {
332 int openflags = O_WRONLY|O_CREAT;
333 if (ml != 1)
334 openflags |= O_TRUNC;
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100335 wfd = ksys_open(collected, openflags, mode);
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (wfd >= 0) {
Dominik Brodowski55731b32018-03-11 11:34:55 +0100338 ksys_fchown(wfd, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100339 ksys_fchmod(wfd, mode);
Randy Robertsond20d5a72009-04-13 14:40:04 -0700340 if (body_len)
Dominik Brodowski411d9472018-03-11 11:34:54 +0100341 ksys_ftruncate(wfd, body_len);
Nye Liu889d51a2008-10-15 22:01:40 -0700342 vcollected = kstrdup(collected, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 state = CopyFile;
344 }
345 }
346 } else if (S_ISDIR(mode)) {
Dominik Brodowski0101db72018-03-11 11:34:49 +0100347 ksys_mkdir(collected, mode);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100348 ksys_chown(collected, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100349 ksys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700350 dir_add(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
352 S_ISFIFO(mode) || S_ISSOCK(mode)) {
353 if (maybe_link() == 0) {
Dominik Brodowski87c4e192018-03-11 11:34:50 +0100354 ksys_mknod(collected, mode, rdev);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100355 ksys_chown(collected, uid, gid);
Dominik Brodowski03450e22018-03-11 11:34:53 +0100356 ksys_chmod(collected, mode);
Nye Liu889d51a2008-10-15 22:01:40 -0700357 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 }
360 return 0;
361}
362
363static int __init do_copy(void)
364{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700365 if (byte_count >= body_len) {
David Engraf9687fd92014-08-08 14:23:16 -0700366 if (xwrite(wfd, victim, body_len) != body_len)
367 error("write error");
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100368 ksys_close(wfd);
Nye Liu889d51a2008-10-15 22:01:40 -0700369 do_utime(vcollected, mtime);
370 kfree(vcollected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 eat(body_len);
372 state = SkipIt;
373 return 0;
374 } else {
Mark Rustadc34d85a2014-10-13 15:54:07 -0700375 if (xwrite(wfd, victim, byte_count) != byte_count)
David Engraf9687fd92014-08-08 14:23:16 -0700376 error("write error");
Mark Rustadc34d85a2014-10-13 15:54:07 -0700377 body_len -= byte_count;
378 eat(byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 1;
380 }
381}
382
383static int __init do_symlink(void)
384{
385 collected[N_ALIGN(name_len) + body_len] = '\0';
H. Peter Anvin2139a7f2006-06-26 00:28:02 -0700386 clean_path(collected, 0);
Dominik Brodowskib724e842018-03-11 11:34:49 +0100387 ksys_symlink(collected + N_ALIGN(name_len), collected);
Dominik Brodowski55731b32018-03-11 11:34:55 +0100388 ksys_lchown(collected, uid, gid);
Nye Liu889d51a2008-10-15 22:01:40 -0700389 do_utime(collected, mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 state = SkipIt;
391 next_state = Reset;
392 return 0;
393}
394
395static __initdata int (*actions[])(void) = {
396 [Start] = do_start,
397 [Collect] = do_collect,
398 [GotHeader] = do_header,
399 [SkipIt] = do_skip,
400 [GotName] = do_name,
401 [CopyFile] = do_copy,
402 [GotSymlink] = do_symlink,
403 [Reset] = do_reset,
404};
405
Yinghai Lud97b07c2014-08-08 14:23:14 -0700406static long __init write_buffer(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Mark Rustadc34d85a2014-10-13 15:54:07 -0700408 byte_count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 victim = buf;
410
411 while (!actions[state]())
412 ;
Mark Rustadc34d85a2014-10-13 15:54:07 -0700413 return len - byte_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Yinghai Lud97b07c2014-08-08 14:23:14 -0700416static long __init flush_buffer(void *bufv, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Alain Knaff30d65db2009-01-04 22:46:17 +0100418 char *buf = (char *) bufv;
Yinghai Lud97b07c2014-08-08 14:23:14 -0700419 long written;
420 long origLen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (message)
Alain Knaff30d65db2009-01-04 22:46:17 +0100422 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 while ((written = write_buffer(buf, len)) < len && !message) {
424 char c = buf[written];
425 if (c == '0') {
426 buf += written;
427 len -= written;
428 state = Start;
429 } else if (c == 0) {
430 buf += written;
431 len -= written;
432 state = Reset;
433 } else
David Engrafe5eed352019-03-07 16:30:19 -0800434 error("junk within compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Alain Knaff30d65db2009-01-04 22:46:17 +0100436 return origLen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Yinghai Lud97b07c2014-08-08 14:23:14 -0700439static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800441#include <linux/decompress/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Yinghai Lud97b07c2014-08-08 14:23:14 -0700443static char * __init unpack_to_rootfs(char *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Yinghai Lud97b07c2014-08-08 14:23:14 -0700445 long written;
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800446 decompress_fn decompress;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800447 const char *compress_name;
448 static __initdata char msg_buf[64];
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800449
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700450 header_buf = kmalloc(110, GFP_KERNEL);
451 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
452 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
Alain Knaff30d65db2009-01-04 22:46:17 +0100453
454 if (!header_buf || !symlink_buf || !name_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 panic("can't allocate buffers");
Alain Knaff30d65db2009-01-04 22:46:17 +0100456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 state = Start;
458 this_header = 0;
459 message = NULL;
460 while (!message && len) {
461 loff_t saved_offset = this_header;
462 if (*buf == '0' && !(this_header & 3)) {
463 state = Start;
464 written = write_buffer(buf, len);
465 buf += written;
466 len -= written;
467 continue;
468 }
469 if (!*buf) {
470 buf++;
471 len--;
472 this_header++;
473 continue;
474 }
475 this_header = 0;
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800476 decompress = decompress_method(buf, len, &compress_name);
Daniel M. Weeks6aa7a292014-04-07 15:39:16 -0700477 pr_debug("Detected %s compressed data\n", compress_name);
Phillip Lougher54291362009-12-14 21:45:19 +0000478 if (decompress) {
Yinghai Lud97b07c2014-08-08 14:23:14 -0700479 int res = decompress(buf, len, NULL, flush_buffer, NULL,
H. Peter Anvin889c92d2009-01-08 15:14:17 -0800480 &my_inptr, error);
Phillip Lougher54291362009-12-14 21:45:19 +0000481 if (res)
482 error("decompressor failed");
483 } else if (compress_name) {
H. Peter Anvin23a22d52009-01-12 14:24:04 -0800484 if (!message) {
485 snprintf(msg_buf, sizeof msg_buf,
486 "compression method %s not configured",
487 compress_name);
488 message = msg_buf;
489 }
Phillip Lougherdf37bd12010-04-23 13:18:11 -0400490 } else
David Engrafe5eed352019-03-07 16:30:19 -0800491 error("invalid magic at start of compressed archive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (state != Reset)
David Engrafe5eed352019-03-07 16:30:19 -0800493 error("junk at the end of compressed archive");
Alain Knaff30d65db2009-01-04 22:46:17 +0100494 this_header = saved_offset + my_inptr;
495 buf += my_inptr;
496 len -= my_inptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Nye Liu889d51a2008-10-15 22:01:40 -0700498 dir_utime();
Thomas Petazzoni3265e662008-04-29 00:59:43 -0700499 kfree(name_buf);
500 kfree(symlink_buf);
501 kfree(header_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return message;
503}
504
Michael Neuling0a7b35cb2007-02-10 01:44:33 -0800505static int __initdata do_retain_initrd;
506
507static int __init retain_initrd_param(char *str)
508{
509 if (*str)
510 return 0;
511 do_retain_initrd = 1;
512 return 1;
513}
514__setup("retain_initrd", retain_initrd_param);
515
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700516extern char __initramfs_start[];
517extern unsigned long __initramfs_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#include <linux/initrd.h>
Haren Myneni9c15e852006-02-10 01:51:05 -0800519#include <linux/kexec.h>
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700520
Dave Young2965faa2015-09-09 15:38:55 -0700521#ifdef CONFIG_KEXEC_CORE
Christoph Hellwig23091e22019-05-13 17:18:21 -0700522static bool kexec_free_initrd(void)
523{
Haren Myneni9c15e852006-02-10 01:51:05 -0800524 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
525 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
526
527 /*
528 * If the initrd region is overlapped with crashkernel reserved region,
529 * free only memory that is not part of crashkernel region.
530 */
Christoph Hellwig23091e22019-05-13 17:18:21 -0700531 if (initrd_start >= crashk_end || initrd_end <= crashk_start)
532 return false;
533
534 /*
535 * Initialize initrd memory region since the kexec boot does not do.
536 */
537 memset((void *)initrd_start, 0, initrd_end - initrd_start);
538 if (initrd_start < crashk_start)
539 free_initrd_mem(initrd_start, crashk_start);
540 if (initrd_end > crashk_end)
541 free_initrd_mem(crashk_end, initrd_end);
542 return true;
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700543}
Christoph Hellwig23091e22019-05-13 17:18:21 -0700544#else
545static inline bool kexec_free_initrd(void)
546{
547 return false;
548}
549#endif /* CONFIG_KEXEC_CORE */
Jan Beulich0f3d2bd2005-09-13 01:25:12 -0700550
Andrew Mortona841c672019-02-20 22:18:52 -0800551#ifdef CONFIG_BLK_DEV_RAM
Li, Shaohuadf520922008-08-13 17:26:01 +0800552#define BUF_SIZE 1024
553static void __init clean_rootfs(void)
554{
555 int fd;
556 void *buf;
557 struct linux_dirent64 *dirp;
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800558 int num;
Li, Shaohuadf520922008-08-13 17:26:01 +0800559
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100560 fd = ksys_open("/", O_RDONLY, 0);
Li, Shaohuadf520922008-08-13 17:26:01 +0800561 WARN_ON(fd < 0);
562 if (fd < 0)
563 return;
564 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
565 WARN_ON(!buf);
566 if (!buf) {
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100567 ksys_close(fd);
Li, Shaohuadf520922008-08-13 17:26:01 +0800568 return;
569 }
570
571 dirp = buf;
Dominik Brodowski454dab32018-03-13 21:34:04 +0100572 num = ksys_getdents64(fd, dirp, BUF_SIZE);
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800573 while (num > 0) {
574 while (num > 0) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700575 struct kstat st;
Li, Shaohuadf520922008-08-13 17:26:01 +0800576 int ret;
577
Arnd Bergmann046aa122017-05-08 15:57:00 -0700578 ret = vfs_lstat(dirp->d_name, &st);
Li, Shaohuadf520922008-08-13 17:26:01 +0800579 WARN_ON_ONCE(ret);
580 if (!ret) {
Arnd Bergmann046aa122017-05-08 15:57:00 -0700581 if (S_ISDIR(st.mode))
Dominik Brodowskif459dffa2018-03-11 11:34:48 +0100582 ksys_rmdir(dirp->d_name);
Li, Shaohuadf520922008-08-13 17:26:01 +0800583 else
Dominik Brodowski0f32ab82018-03-11 11:34:47 +0100584 ksys_unlink(dirp->d_name);
Li, Shaohuadf520922008-08-13 17:26:01 +0800585 }
586
H Hartley Sweeten8aaed5b2010-03-05 13:42:39 -0800587 num -= dirp->d_reclen;
Li, Shaohuadf520922008-08-13 17:26:01 +0800588 dirp = (void *)dirp + dirp->d_reclen;
589 }
590 dirp = buf;
591 memset(buf, 0, BUF_SIZE);
Dominik Brodowski454dab32018-03-13 21:34:04 +0100592 num = ksys_getdents64(fd, dirp, BUF_SIZE);
Li, Shaohuadf520922008-08-13 17:26:01 +0800593 }
594
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100595 ksys_close(fd);
Li, Shaohuadf520922008-08-13 17:26:01 +0800596 kfree(buf);
597}
Andrew Mortona841c672019-02-20 22:18:52 -0800598#endif
Li, Shaohuadf520922008-08-13 17:26:01 +0800599
Linus Torvalds9a9e0d62008-03-15 11:53:32 -0700600static int __init populate_rootfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Stafford Horne17a9be32017-05-04 21:15:56 +0900602 /* Load the built in initramfs */
Hendrik Bruecknerffe80182010-09-17 15:24:11 -0700603 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (err)
Tetsuo Handa499a4582014-01-23 15:54:56 -0800605 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
Stafford Horne17a9be32017-05-04 21:15:56 +0900606 /* If available load the bootloader supplied initrd */
Daniel Thompsoncff75e02017-05-08 15:56:57 -0700607 if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800608#ifdef CONFIG_BLK_DEV_RAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int fd;
Eric Piela1e6b6c2009-05-06 16:03:06 -0700610 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 err = unpack_to_rootfs((char *)initrd_start,
Li, Shaohuadf520922008-08-13 17:26:01 +0800612 initrd_end - initrd_start);
Christoph Hellwig54c7a892019-05-13 17:18:17 -0700613 if (!err)
Tejun Heobb813f42013-01-18 14:05:56 -0800614 goto done;
Christoph Hellwig54c7a892019-05-13 17:18:17 -0700615
616 clean_rootfs();
617 unpack_to_rootfs(__initramfs_start, __initramfs_size);
618
Simon Kitchingc1c490e2009-04-02 16:57:00 -0700619 printk(KERN_INFO "rootfs image is not initramfs (%s)"
620 "; looks like an initrd\n", err);
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100621 fd = ksys_open("/initrd.image",
Namhyung Kim562f5e62010-10-26 14:22:42 -0700622 O_WRONLY|O_CREAT, 0700);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (fd >= 0) {
Yinghai Lu38747432014-08-08 14:23:12 -0700624 ssize_t written = xwrite(fd, (char *)initrd_start,
625 initrd_end - initrd_start);
626
627 if (written != initrd_end - initrd_start)
628 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
629 written, initrd_end - initrd_start);
630
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100631 ksys_close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Tejun Heobb813f42013-01-18 14:05:56 -0800633 done:
Linus Torvalds394e4f52017-05-06 10:27:13 -0700634 /* empty statement */;
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800635#else
Eric Piela1e6b6c2009-05-06 16:03:06 -0700636 printk(KERN_INFO "Unpacking initramfs...\n");
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800637 err = unpack_to_rootfs((char *)initrd_start,
Li, Shaohuadf520922008-08-13 17:26:01 +0800638 initrd_end - initrd_start);
Andrew Mortona841c672019-02-20 22:18:52 -0800639 if (err)
Eric Piela1e6b6c2009-05-06 16:03:06 -0700640 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
Zdenek Pavlas340e48e2006-03-25 03:07:49 -0800641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Christoph Hellwig23091e22019-05-13 17:18:21 -0700643
644 /*
645 * If the initrd region is overlapped with crashkernel reserved region,
646 * free only memory that is not part of crashkernel region.
647 */
648 if (!do_retain_initrd && !kexec_free_initrd())
649 free_initrd_mem(initrd_start, initrd_end);
650 initrd_start = 0;
651 initrd_end = 0;
652
Stafford Horne17a9be32017-05-04 21:15:56 +0900653 flush_delayed_fput();
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
Linus Torvalds8d610dd2006-12-11 12:12:04 -0800656rootfs_initcall(populate_rootfs);