remove libdss from Makefile
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / init / initramfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
12 #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>
19 #include <linux/dirent.h>
20 #include <linux/syscalls.h>
21 #include <linux/utime.h>
22 #include <linux/file.h>
23 #include <linux/initramfs.h>
24
25 static ssize_t __init xwrite(int fd, const char *p, size_t count)
26 {
27 ssize_t out = 0;
28
29 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
30 while (count) {
31 ssize_t rv = sys_write(fd, p, count);
32
33 if (rv < 0) {
34 if (rv == -EINTR || rv == -EAGAIN)
35 continue;
36 return out ? out : rv;
37 } else if (rv == 0)
38 break;
39
40 p += rv;
41 out += rv;
42 count -= rv;
43 }
44
45 return out;
46 }
47
48 static __initdata char *message;
49 static void __init error(char *x)
50 {
51 if (!message)
52 message = x;
53 }
54
55 /* link hash */
56
57 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
58
59 static __initdata struct hash {
60 int ino, minor, major;
61 umode_t mode;
62 struct hash *next;
63 char name[N_ALIGN(PATH_MAX)];
64 } *head[32];
65
66 static inline int hash(int major, int minor, int ino)
67 {
68 unsigned long tmp = ino + minor + (major << 3);
69 tmp += tmp >> 5;
70 return tmp & 31;
71 }
72
73 static char __init *find_link(int major, int minor, int ino,
74 umode_t mode, char *name)
75 {
76 struct hash **p, *q;
77 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
78 if ((*p)->ino != ino)
79 continue;
80 if ((*p)->minor != minor)
81 continue;
82 if ((*p)->major != major)
83 continue;
84 if (((*p)->mode ^ mode) & S_IFMT)
85 continue;
86 return (*p)->name;
87 }
88 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
89 if (!q)
90 panic("can't allocate link hash entry");
91 q->major = major;
92 q->minor = minor;
93 q->ino = ino;
94 q->mode = mode;
95 strcpy(q->name, name);
96 q->next = NULL;
97 *p = q;
98 return NULL;
99 }
100
101 static void __init free_hash(void)
102 {
103 struct hash **p, *q;
104 for (p = head; p < head + 32; p++) {
105 while (*p) {
106 q = *p;
107 *p = q->next;
108 kfree(q);
109 }
110 }
111 }
112
113 static long __init do_utime(char *filename, time_t mtime)
114 {
115 struct timespec64 t[2];
116
117 t[0].tv_sec = mtime;
118 t[0].tv_nsec = 0;
119 t[1].tv_sec = mtime;
120 t[1].tv_nsec = 0;
121
122 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
123 }
124
125 static __initdata LIST_HEAD(dir_list);
126 struct dir_entry {
127 struct list_head list;
128 char *name;
129 time_t mtime;
130 };
131
132 static void __init dir_add(const char *name, time_t mtime)
133 {
134 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
135 if (!de)
136 panic("can't allocate dir_entry buffer");
137 INIT_LIST_HEAD(&de->list);
138 de->name = kstrdup(name, GFP_KERNEL);
139 de->mtime = mtime;
140 list_add(&de->list, &dir_list);
141 }
142
143 static void __init dir_utime(void)
144 {
145 struct dir_entry *de, *tmp;
146 list_for_each_entry_safe(de, tmp, &dir_list, list) {
147 list_del(&de->list);
148 do_utime(de->name, de->mtime);
149 kfree(de->name);
150 kfree(de);
151 }
152 }
153
154 static __initdata time_t mtime;
155
156 /* cpio header parsing */
157
158 static __initdata unsigned long ino, major, minor, nlink;
159 static __initdata umode_t mode;
160 static __initdata unsigned long body_len, name_len;
161 static __initdata uid_t uid;
162 static __initdata gid_t gid;
163 static __initdata unsigned rdev;
164
165 static void __init parse_header(char *s)
166 {
167 unsigned long parsed[12];
168 char buf[9];
169 int i;
170
171 buf[8] = '\0';
172 for (i = 0, s += 6; i < 12; i++, s += 8) {
173 memcpy(buf, s, 8);
174 parsed[i] = simple_strtoul(buf, NULL, 16);
175 }
176 ino = parsed[0];
177 mode = parsed[1];
178 uid = parsed[2];
179 gid = parsed[3];
180 nlink = parsed[4];
181 mtime = parsed[5];
182 body_len = parsed[6];
183 major = parsed[7];
184 minor = parsed[8];
185 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
186 name_len = parsed[11];
187 }
188
189 /* FSM */
190
191 static __initdata enum state {
192 Start,
193 Collect,
194 GotHeader,
195 SkipIt,
196 GotName,
197 CopyFile,
198 GotSymlink,
199 Reset
200 } state, next_state;
201
202 static __initdata char *victim;
203 static unsigned long byte_count __initdata;
204 static __initdata loff_t this_header, next_header;
205
206 static inline void __init eat(unsigned n)
207 {
208 victim += n;
209 this_header += n;
210 byte_count -= n;
211 }
212
213 static __initdata char *vcollected;
214 static __initdata char *collected;
215 static long remains __initdata;
216 static __initdata char *collect;
217
218 static void __init read_into(char *buf, unsigned size, enum state next)
219 {
220 if (byte_count >= size) {
221 collected = victim;
222 eat(size);
223 state = next;
224 } else {
225 collect = collected = buf;
226 remains = size;
227 next_state = next;
228 state = Collect;
229 }
230 }
231
232 static __initdata char *header_buf, *symlink_buf, *name_buf;
233
234 static int __init do_start(void)
235 {
236 read_into(header_buf, 110, GotHeader);
237 return 0;
238 }
239
240 static int __init do_collect(void)
241 {
242 unsigned long n = remains;
243 if (byte_count < n)
244 n = byte_count;
245 memcpy(collect, victim, n);
246 eat(n);
247 collect += n;
248 if ((remains -= n) != 0)
249 return 1;
250 state = next_state;
251 return 0;
252 }
253
254 static int __init do_header(void)
255 {
256 if (memcmp(collected, "070707", 6)==0) {
257 error("incorrect cpio method used: use -H newc option");
258 return 1;
259 }
260 if (memcmp(collected, "070701", 6)) {
261 error("no cpio magic");
262 return 1;
263 }
264 parse_header(collected);
265 next_header = this_header + N_ALIGN(name_len) + body_len;
266 next_header = (next_header + 3) & ~3;
267 state = SkipIt;
268 if (name_len <= 0 || name_len > PATH_MAX)
269 return 0;
270 if (S_ISLNK(mode)) {
271 if (body_len > PATH_MAX)
272 return 0;
273 collect = collected = symlink_buf;
274 remains = N_ALIGN(name_len) + body_len;
275 next_state = GotSymlink;
276 state = Collect;
277 return 0;
278 }
279 if (S_ISREG(mode) || !body_len)
280 read_into(name_buf, N_ALIGN(name_len), GotName);
281 return 0;
282 }
283
284 static int __init do_skip(void)
285 {
286 if (this_header + byte_count < next_header) {
287 eat(byte_count);
288 return 1;
289 } else {
290 eat(next_header - this_header);
291 state = next_state;
292 return 0;
293 }
294 }
295
296 static int __init do_reset(void)
297 {
298 while (byte_count && *victim == '\0')
299 eat(1);
300 if (byte_count && (this_header & 3))
301 error("broken padding");
302 return 1;
303 }
304
305 static int __init maybe_link(void)
306 {
307 if (nlink >= 2) {
308 char *old = find_link(major, minor, ino, mode, collected);
309 if (old)
310 return (sys_link(old, collected) < 0) ? -1 : 1;
311 }
312 return 0;
313 }
314
315 static void __init clean_path(char *path, umode_t fmode)
316 {
317 struct kstat st;
318
319 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
320 if (S_ISDIR(st.mode))
321 sys_rmdir(path);
322 else
323 sys_unlink(path);
324 }
325 }
326
327 static __initdata int wfd;
328
329 static int __init do_name(void)
330 {
331 state = SkipIt;
332 next_state = Reset;
333 if (strcmp(collected, "TRAILER!!!") == 0) {
334 free_hash();
335 return 0;
336 }
337 clean_path(collected, mode);
338 if (S_ISREG(mode)) {
339 int ml = maybe_link();
340 if (ml >= 0) {
341 int openflags = O_WRONLY|O_CREAT;
342 if (ml != 1)
343 openflags |= O_TRUNC;
344 wfd = sys_open(collected, openflags, mode);
345
346 if (wfd >= 0) {
347 sys_fchown(wfd, uid, gid);
348 sys_fchmod(wfd, mode);
349 if (body_len)
350 sys_ftruncate(wfd, body_len);
351 vcollected = kstrdup(collected, GFP_KERNEL);
352 state = CopyFile;
353 }
354 }
355 } else if (S_ISDIR(mode)) {
356 sys_mkdir(collected, mode);
357 sys_chown(collected, uid, gid);
358 sys_chmod(collected, mode);
359 dir_add(collected, mtime);
360 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
361 S_ISFIFO(mode) || S_ISSOCK(mode)) {
362 if (maybe_link() == 0) {
363 sys_mknod(collected, mode, rdev);
364 sys_chown(collected, uid, gid);
365 sys_chmod(collected, mode);
366 do_utime(collected, mtime);
367 }
368 }
369 return 0;
370 }
371
372 static int __init do_copy(void)
373 {
374 if (byte_count >= body_len) {
375 if (xwrite(wfd, victim, body_len) != body_len)
376 error("write error");
377 sys_close(wfd);
378 do_utime(vcollected, mtime);
379 kfree(vcollected);
380 eat(body_len);
381 state = SkipIt;
382 return 0;
383 } else {
384 if (xwrite(wfd, victim, byte_count) != byte_count)
385 error("write error");
386 body_len -= byte_count;
387 eat(byte_count);
388 return 1;
389 }
390 }
391
392 static int __init do_symlink(void)
393 {
394 collected[N_ALIGN(name_len) + body_len] = '\0';
395 clean_path(collected, 0);
396 sys_symlink(collected + N_ALIGN(name_len), collected);
397 sys_lchown(collected, uid, gid);
398 do_utime(collected, mtime);
399 state = SkipIt;
400 next_state = Reset;
401 return 0;
402 }
403
404 static __initdata int (*actions[])(void) = {
405 [Start] = do_start,
406 [Collect] = do_collect,
407 [GotHeader] = do_header,
408 [SkipIt] = do_skip,
409 [GotName] = do_name,
410 [CopyFile] = do_copy,
411 [GotSymlink] = do_symlink,
412 [Reset] = do_reset,
413 };
414
415 static long __init write_buffer(char *buf, unsigned long len)
416 {
417 byte_count = len;
418 victim = buf;
419
420 while (!actions[state]())
421 ;
422 return len - byte_count;
423 }
424
425 static long __init flush_buffer(void *bufv, unsigned long len)
426 {
427 char *buf = (char *) bufv;
428 long written;
429 long origLen = len;
430 if (message)
431 return -1;
432 while ((written = write_buffer(buf, len)) < len && !message) {
433 char c = buf[written];
434 if (c == '0') {
435 buf += written;
436 len -= written;
437 state = Start;
438 } else if (c == 0) {
439 buf += written;
440 len -= written;
441 state = Reset;
442 } else
443 error("junk in compressed archive");
444 }
445 return origLen;
446 }
447
448 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
449
450 #include <linux/decompress/generic.h>
451
452 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
453 {
454 long written;
455 decompress_fn decompress;
456 const char *compress_name;
457 static __initdata char msg_buf[64];
458
459 header_buf = kmalloc(110, GFP_KERNEL);
460 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
461 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
462
463 if (!header_buf || !symlink_buf || !name_buf)
464 panic("can't allocate buffers");
465
466 state = Start;
467 this_header = 0;
468 message = NULL;
469 while (!message && len) {
470 loff_t saved_offset = this_header;
471 if (*buf == '0' && !(this_header & 3)) {
472 state = Start;
473 written = write_buffer(buf, len);
474 buf += written;
475 len -= written;
476 continue;
477 }
478 if (!*buf) {
479 buf++;
480 len--;
481 this_header++;
482 continue;
483 }
484 this_header = 0;
485 decompress = decompress_method(buf, len, &compress_name);
486 pr_debug("Detected %s compressed data\n", compress_name);
487 if (decompress) {
488 int res = decompress(buf, len, NULL, flush_buffer, NULL,
489 &my_inptr, error);
490 if (res)
491 error("decompressor failed");
492 } else if (compress_name) {
493 if (!message) {
494 snprintf(msg_buf, sizeof msg_buf,
495 "compression method %s not configured",
496 compress_name);
497 message = msg_buf;
498 }
499 } else
500 error("junk in compressed archive");
501 if (state != Reset)
502 error("junk in compressed archive");
503 this_header = saved_offset + my_inptr;
504 buf += my_inptr;
505 len -= my_inptr;
506 }
507 dir_utime();
508 kfree(name_buf);
509 kfree(symlink_buf);
510 kfree(header_buf);
511 return message;
512 }
513
514 static int __initdata do_retain_initrd;
515
516 static int __init retain_initrd_param(char *str)
517 {
518 if (*str)
519 return 0;
520 do_retain_initrd = 1;
521 return 1;
522 }
523 __setup("retain_initrd", retain_initrd_param);
524
525 extern char __initramfs_start[];
526 extern unsigned long __initramfs_size;
527 #include <linux/initrd.h>
528 #include <linux/kexec.h>
529
530 static void __init free_initrd(void)
531 {
532 #ifdef CONFIG_KEXEC_CORE
533 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
534 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
535 #endif
536 if (do_retain_initrd)
537 goto skip;
538
539 #ifdef CONFIG_KEXEC_CORE
540 /*
541 * If the initrd region is overlapped with crashkernel reserved region,
542 * free only memory that is not part of crashkernel region.
543 */
544 if (initrd_start < crashk_end && initrd_end > crashk_start) {
545 /*
546 * Initialize initrd memory region since the kexec boot does
547 * not do.
548 */
549 memset((void *)initrd_start, 0, initrd_end - initrd_start);
550 if (initrd_start < crashk_start)
551 free_initrd_mem(initrd_start, crashk_start);
552 if (initrd_end > crashk_end)
553 free_initrd_mem(crashk_end, initrd_end);
554 } else
555 #endif
556 free_initrd_mem(initrd_start, initrd_end);
557 skip:
558 initrd_start = 0;
559 initrd_end = 0;
560 }
561
562 #ifdef CONFIG_BLK_DEV_RAM
563 #define BUF_SIZE 1024
564 static void __init clean_rootfs(void)
565 {
566 int fd;
567 void *buf;
568 struct linux_dirent64 *dirp;
569 int num;
570
571 fd = sys_open("/", O_RDONLY, 0);
572 WARN_ON(fd < 0);
573 if (fd < 0)
574 return;
575 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
576 WARN_ON(!buf);
577 if (!buf) {
578 sys_close(fd);
579 return;
580 }
581
582 dirp = buf;
583 num = sys_getdents64(fd, dirp, BUF_SIZE);
584 while (num > 0) {
585 while (num > 0) {
586 struct kstat st;
587 int ret;
588
589 ret = vfs_lstat(dirp->d_name, &st);
590 WARN_ON_ONCE(ret);
591 if (!ret) {
592 if (S_ISDIR(st.mode))
593 sys_rmdir(dirp->d_name);
594 else
595 sys_unlink(dirp->d_name);
596 }
597
598 num -= dirp->d_reclen;
599 dirp = (void *)dirp + dirp->d_reclen;
600 }
601 dirp = buf;
602 memset(buf, 0, BUF_SIZE);
603 num = sys_getdents64(fd, dirp, BUF_SIZE);
604 }
605
606 sys_close(fd);
607 kfree(buf);
608 }
609 #endif
610
611 static int __initdata do_skip_initramfs;
612
613 static int __init skip_initramfs_param(char *str)
614 {
615 if (*str)
616 return 0;
617 do_skip_initramfs = 1;
618 return 1;
619 }
620 __setup("skip_initramfs", skip_initramfs_param);
621
622 static int __init populate_rootfs(void)
623 {
624 char *err;
625
626 // if (do_skip_initramfs) {
627 // if (initrd_start)
628 // free_initrd();
629 // return default_rootfs();
630 // }
631
632 /* Load the built in initramfs */
633 err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
634 if (err)
635 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
636 /* If available load the bootloader supplied initrd */
637 if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
638 #ifdef CONFIG_BLK_DEV_RAM
639 int fd;
640 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
641 err = unpack_to_rootfs((char *)initrd_start,
642 initrd_end - initrd_start);
643 if (!err) {
644 free_initrd();
645 goto done;
646 } else {
647 clean_rootfs();
648 unpack_to_rootfs(__initramfs_start, __initramfs_size);
649 }
650 printk(KERN_INFO "rootfs image is not initramfs (%s)"
651 "; looks like an initrd\n", err);
652 fd = sys_open("/initrd.image",
653 O_WRONLY|O_CREAT, 0700);
654 if (fd >= 0) {
655 ssize_t written = xwrite(fd, (char *)initrd_start,
656 initrd_end - initrd_start);
657
658 if (written != initrd_end - initrd_start)
659 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
660 written, initrd_end - initrd_start);
661
662 sys_close(fd);
663 free_initrd();
664 }
665 done:
666 /* empty statement */;
667 #else
668 printk(KERN_INFO "Unpacking initramfs...\n");
669 err = unpack_to_rootfs((char *)initrd_start,
670 initrd_end - initrd_start);
671 if (err)
672 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
673 free_initrd();
674 #endif
675 }
676 flush_delayed_fput();
677 /*
678 * Try loading default modules from initramfs. This gives
679 * us a chance to load before device_initcalls.
680 */
681 load_default_modules();
682
683 return 0;
684 }
685 rootfs_initcall(populate_rootfs);