Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #include <linux/init.h> |
Rasmus Villemoes | e7cb072 | 2021-05-06 18:05:42 -0700 | [diff] [blame] | 3 | #include <linux/async.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/fs.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/types.h> |
| 7 | #include <linux/fcntl.h> |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/string.h> |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 10 | #include <linux/dirent.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/syscalls.h> |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 12 | #include <linux/utime.h> |
Lokesh Vutla | 0886551 | 2017-02-27 14:28:12 -0800 | [diff] [blame] | 13 | #include <linux/file.h> |
Mike Rapoport | 899ee4a | 2019-09-28 11:02:26 +0300 | [diff] [blame] | 14 | #include <linux/memblock.h> |
Florian Fainelli | dd23e80 | 2021-02-25 17:22:46 -0800 | [diff] [blame] | 15 | #include <linux/mm.h> |
Christoph Hellwig | b2a74d5 | 2020-06-06 13:59:54 +0200 | [diff] [blame] | 16 | #include <linux/namei.h> |
Christoph Hellwig | 8fb9f73 | 2020-07-23 08:23:40 +0200 | [diff] [blame] | 17 | #include <linux/init_syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 19 | static ssize_t __init xwrite(struct file *file, const char *p, size_t count, |
| 20 | loff_t *pos) |
Yinghai Lu | 3874743 | 2014-08-08 14:23:12 -0700 | [diff] [blame] | 21 | { |
| 22 | ssize_t out = 0; |
| 23 | |
| 24 | /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ |
| 25 | while (count) { |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 26 | ssize_t rv = kernel_write(file, p, count, pos); |
Yinghai Lu | 3874743 | 2014-08-08 14:23:12 -0700 | [diff] [blame] | 27 | |
| 28 | if (rv < 0) { |
| 29 | if (rv == -EINTR || rv == -EAGAIN) |
| 30 | continue; |
| 31 | return out ? out : rv; |
| 32 | } else if (rv == 0) |
| 33 | break; |
| 34 | |
| 35 | p += rv; |
| 36 | out += rv; |
| 37 | count -= rv; |
| 38 | } |
| 39 | |
| 40 | return out; |
| 41 | } |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static __initdata char *message; |
| 44 | static void __init error(char *x) |
| 45 | { |
| 46 | if (!message) |
| 47 | message = x; |
| 48 | } |
| 49 | |
Florian Fainelli | dd23e80 | 2021-02-25 17:22:46 -0800 | [diff] [blame] | 50 | static void panic_show_mem(const char *fmt, ...) |
| 51 | { |
| 52 | va_list args; |
| 53 | |
| 54 | show_mem(0, NULL); |
| 55 | va_start(args, fmt); |
| 56 | panic(fmt, args); |
| 57 | va_end(args); |
| 58 | } |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* link hash */ |
| 61 | |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 62 | #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static __initdata struct hash { |
| 65 | int ino, minor, major; |
Al Viro | 685dd2d | 2011-07-26 04:34:13 -0400 | [diff] [blame] | 66 | umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | struct hash *next; |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 68 | char name[N_ALIGN(PATH_MAX)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } *head[32]; |
| 70 | |
| 71 | static inline int hash(int major, int minor, int ino) |
| 72 | { |
| 73 | unsigned long tmp = ino + minor + (major << 3); |
| 74 | tmp += tmp >> 5; |
| 75 | return tmp & 31; |
| 76 | } |
| 77 | |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 78 | static char __init *find_link(int major, int minor, int ino, |
Al Viro | 685dd2d | 2011-07-26 04:34:13 -0400 | [diff] [blame] | 79 | umode_t mode, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | struct hash **p, *q; |
| 82 | for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { |
| 83 | if ((*p)->ino != ino) |
| 84 | continue; |
| 85 | if ((*p)->minor != minor) |
| 86 | continue; |
| 87 | if ((*p)->major != major) |
| 88 | continue; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 89 | if (((*p)->mode ^ mode) & S_IFMT) |
| 90 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return (*p)->name; |
| 92 | } |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 93 | q = kmalloc(sizeof(struct hash), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (!q) |
Florian Fainelli | dd23e80 | 2021-02-25 17:22:46 -0800 | [diff] [blame] | 95 | panic_show_mem("can't allocate link hash entry"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | q->major = major; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 97 | q->minor = minor; |
| 98 | q->ino = ino; |
| 99 | q->mode = mode; |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 100 | strcpy(q->name, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | q->next = NULL; |
| 102 | *p = q; |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | static void __init free_hash(void) |
| 107 | { |
| 108 | struct hash **p, *q; |
| 109 | for (p = head; p < head + 32; p++) { |
| 110 | while (*p) { |
| 111 | q = *p; |
| 112 | *p = q->next; |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 113 | kfree(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Arnd Bergmann | e35c4c6 | 2017-11-17 15:31:04 -0800 | [diff] [blame] | 118 | static long __init do_utime(char *filename, time64_t mtime) |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 119 | { |
Deepa Dinamani | aaed2dd | 2017-08-02 19:51:15 -0700 | [diff] [blame] | 120 | struct timespec64 t[2]; |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 121 | |
| 122 | t[0].tv_sec = mtime; |
| 123 | t[0].tv_nsec = 0; |
| 124 | t[1].tv_sec = mtime; |
| 125 | t[1].tv_nsec = 0; |
Christoph Hellwig | 235e579 | 2020-07-21 16:05:31 +0200 | [diff] [blame] | 126 | return init_utimes(filename, t); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static __initdata LIST_HEAD(dir_list); |
| 130 | struct dir_entry { |
| 131 | struct list_head list; |
| 132 | char *name; |
Arnd Bergmann | e35c4c6 | 2017-11-17 15:31:04 -0800 | [diff] [blame] | 133 | time64_t mtime; |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Arnd Bergmann | e35c4c6 | 2017-11-17 15:31:04 -0800 | [diff] [blame] | 136 | static void __init dir_add(const char *name, time64_t mtime) |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 137 | { |
| 138 | struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL); |
| 139 | if (!de) |
Florian Fainelli | dd23e80 | 2021-02-25 17:22:46 -0800 | [diff] [blame] | 140 | panic_show_mem("can't allocate dir_entry buffer"); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 141 | INIT_LIST_HEAD(&de->list); |
| 142 | de->name = kstrdup(name, GFP_KERNEL); |
| 143 | de->mtime = mtime; |
| 144 | list_add(&de->list, &dir_list); |
| 145 | } |
| 146 | |
| 147 | static void __init dir_utime(void) |
| 148 | { |
| 149 | struct dir_entry *de, *tmp; |
| 150 | list_for_each_entry_safe(de, tmp, &dir_list, list) { |
| 151 | list_del(&de->list); |
| 152 | do_utime(de->name, de->mtime); |
| 153 | kfree(de->name); |
| 154 | kfree(de); |
| 155 | } |
| 156 | } |
| 157 | |
Arnd Bergmann | e35c4c6 | 2017-11-17 15:31:04 -0800 | [diff] [blame] | 158 | static __initdata time64_t mtime; |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | /* cpio header parsing */ |
| 161 | |
| 162 | static __initdata unsigned long ino, major, minor, nlink; |
Al Viro | 685dd2d | 2011-07-26 04:34:13 -0400 | [diff] [blame] | 163 | static __initdata umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | static __initdata unsigned long body_len, name_len; |
| 165 | static __initdata uid_t uid; |
| 166 | static __initdata gid_t gid; |
| 167 | static __initdata unsigned rdev; |
| 168 | |
| 169 | static void __init parse_header(char *s) |
| 170 | { |
| 171 | unsigned long parsed[12]; |
| 172 | char buf[9]; |
| 173 | int i; |
| 174 | |
| 175 | buf[8] = '\0'; |
| 176 | for (i = 0, s += 6; i < 12; i++, s += 8) { |
| 177 | memcpy(buf, s, 8); |
| 178 | parsed[i] = simple_strtoul(buf, NULL, 16); |
| 179 | } |
| 180 | ino = parsed[0]; |
| 181 | mode = parsed[1]; |
| 182 | uid = parsed[2]; |
| 183 | gid = parsed[3]; |
| 184 | nlink = parsed[4]; |
Arnd Bergmann | e35c4c6 | 2017-11-17 15:31:04 -0800 | [diff] [blame] | 185 | mtime = parsed[5]; /* breaks in y2106 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | body_len = parsed[6]; |
| 187 | major = parsed[7]; |
| 188 | minor = parsed[8]; |
| 189 | rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); |
| 190 | name_len = parsed[11]; |
| 191 | } |
| 192 | |
| 193 | /* FSM */ |
| 194 | |
| 195 | static __initdata enum state { |
| 196 | Start, |
| 197 | Collect, |
| 198 | GotHeader, |
| 199 | SkipIt, |
| 200 | GotName, |
| 201 | CopyFile, |
| 202 | GotSymlink, |
| 203 | Reset |
| 204 | } state, next_state; |
| 205 | |
| 206 | static __initdata char *victim; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 207 | static unsigned long byte_count __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | static __initdata loff_t this_header, next_header; |
| 209 | |
Al Viro | b0a5ab9 | 2007-07-26 17:33:59 +0100 | [diff] [blame] | 210 | static inline void __init eat(unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | victim += n; |
| 213 | this_header += n; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 214 | byte_count -= n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | static __initdata char *collected; |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 218 | static long remains __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | static __initdata char *collect; |
| 220 | |
| 221 | static void __init read_into(char *buf, unsigned size, enum state next) |
| 222 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 223 | if (byte_count >= size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | collected = victim; |
| 225 | eat(size); |
| 226 | state = next; |
| 227 | } else { |
| 228 | collect = collected = buf; |
| 229 | remains = size; |
| 230 | next_state = next; |
| 231 | state = Collect; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static __initdata char *header_buf, *symlink_buf, *name_buf; |
| 236 | |
| 237 | static int __init do_start(void) |
| 238 | { |
| 239 | read_into(header_buf, 110, GotHeader); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int __init do_collect(void) |
| 244 | { |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 245 | unsigned long n = remains; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 246 | if (byte_count < n) |
| 247 | n = byte_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | memcpy(collect, victim, n); |
| 249 | eat(n); |
| 250 | collect += n; |
| 251 | if ((remains -= n) != 0) |
| 252 | return 1; |
| 253 | state = next_state; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int __init do_header(void) |
| 258 | { |
Arjan van de Ven | 2e591bb | 2006-12-06 20:37:19 -0800 | [diff] [blame] | 259 | if (memcmp(collected, "070707", 6)==0) { |
| 260 | error("incorrect cpio method used: use -H newc option"); |
| 261 | return 1; |
| 262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (memcmp(collected, "070701", 6)) { |
| 264 | error("no cpio magic"); |
| 265 | return 1; |
| 266 | } |
| 267 | parse_header(collected); |
| 268 | next_header = this_header + N_ALIGN(name_len) + body_len; |
| 269 | next_header = (next_header + 3) & ~3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | state = SkipIt; |
| 271 | if (name_len <= 0 || name_len > PATH_MAX) |
| 272 | return 0; |
| 273 | if (S_ISLNK(mode)) { |
| 274 | if (body_len > PATH_MAX) |
| 275 | return 0; |
| 276 | collect = collected = symlink_buf; |
| 277 | remains = N_ALIGN(name_len) + body_len; |
| 278 | next_state = GotSymlink; |
| 279 | state = Collect; |
| 280 | return 0; |
| 281 | } |
| 282 | if (S_ISREG(mode) || !body_len) |
| 283 | read_into(name_buf, N_ALIGN(name_len), GotName); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int __init do_skip(void) |
| 288 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 289 | if (this_header + byte_count < next_header) { |
| 290 | eat(byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return 1; |
| 292 | } else { |
| 293 | eat(next_header - this_header); |
| 294 | state = next_state; |
| 295 | return 0; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static int __init do_reset(void) |
| 300 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 301 | while (byte_count && *victim == '\0') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | eat(1); |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 303 | if (byte_count && (this_header & 3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | error("broken padding"); |
| 305 | return 1; |
| 306 | } |
| 307 | |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 308 | static void __init clean_path(char *path, umode_t fmode) |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 309 | { |
Arnd Bergmann | 046aa12 | 2017-05-08 15:57:00 -0700 | [diff] [blame] | 310 | struct kstat st; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 311 | |
Barret Rhoden | 7b81ce7 | 2020-09-04 09:53:32 -0400 | [diff] [blame] | 312 | if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) && |
Christoph Hellwig | 716308a | 2020-07-22 11:15:40 +0200 | [diff] [blame] | 313 | (st.mode ^ fmode) & S_IFMT) { |
Arnd Bergmann | 046aa12 | 2017-05-08 15:57:00 -0700 | [diff] [blame] | 314 | if (S_ISDIR(st.mode)) |
Christoph Hellwig | 20cce02 | 2020-07-22 11:11:45 +0200 | [diff] [blame] | 315 | init_rmdir(path); |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 316 | else |
Christoph Hellwig | 8fb9f73 | 2020-07-23 08:23:40 +0200 | [diff] [blame] | 317 | init_unlink(path); |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Li Zhijian | 7c0950d | 2018-11-30 14:10:09 -0800 | [diff] [blame] | 321 | static int __init maybe_link(void) |
| 322 | { |
| 323 | if (nlink >= 2) { |
| 324 | char *old = find_link(major, minor, ino, mode, collected); |
| 325 | if (old) { |
| 326 | clean_path(collected, 0); |
Christoph Hellwig | 812931d | 2020-07-22 11:14:19 +0200 | [diff] [blame] | 327 | return (init_link(old, collected) < 0) ? -1 : 1; |
Li Zhijian | 7c0950d | 2018-11-30 14:10:09 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 333 | static __initdata struct file *wfile; |
| 334 | static __initdata loff_t wfile_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | static int __init do_name(void) |
| 337 | { |
| 338 | state = SkipIt; |
| 339 | next_state = Reset; |
| 340 | if (strcmp(collected, "TRAILER!!!") == 0) { |
| 341 | free_hash(); |
| 342 | return 0; |
| 343 | } |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 344 | clean_path(collected, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (S_ISREG(mode)) { |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 346 | int ml = maybe_link(); |
| 347 | if (ml >= 0) { |
| 348 | int openflags = O_WRONLY|O_CREAT; |
| 349 | if (ml != 1) |
| 350 | openflags |= O_TRUNC; |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 351 | wfile = filp_open(collected, openflags, mode); |
| 352 | if (IS_ERR(wfile)) |
| 353 | return 0; |
| 354 | wfile_pos = 0; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 355 | |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 356 | vfs_fchown(wfile, uid, gid); |
| 357 | vfs_fchmod(wfile, mode); |
| 358 | if (body_len) |
| 359 | vfs_truncate(&wfile->f_path, body_len); |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 360 | state = CopyFile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | } else if (S_ISDIR(mode)) { |
Christoph Hellwig | 83ff98c | 2020-07-22 11:14:59 +0200 | [diff] [blame] | 363 | init_mkdir(collected, mode); |
Christoph Hellwig | b873498 | 2020-07-22 11:13:26 +0200 | [diff] [blame] | 364 | init_chown(collected, uid, gid, 0); |
Christoph Hellwig | 1097742 | 2020-07-22 11:41:02 +0200 | [diff] [blame] | 365 | init_chmod(collected, mode); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 366 | dir_add(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } else if (S_ISBLK(mode) || S_ISCHR(mode) || |
| 368 | S_ISFIFO(mode) || S_ISSOCK(mode)) { |
| 369 | if (maybe_link() == 0) { |
Christoph Hellwig | 5fee64f | 2020-07-22 11:41:20 +0200 | [diff] [blame] | 370 | init_mknod(collected, mode, rdev); |
Christoph Hellwig | b873498 | 2020-07-22 11:13:26 +0200 | [diff] [blame] | 371 | init_chown(collected, uid, gid, 0); |
Christoph Hellwig | 1097742 | 2020-07-22 11:41:02 +0200 | [diff] [blame] | 372 | init_chmod(collected, mode); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 373 | do_utime(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int __init do_copy(void) |
| 380 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 381 | if (byte_count >= body_len) { |
Christoph Hellwig | 38b0822 | 2020-07-30 08:19:00 +0200 | [diff] [blame] | 382 | struct timespec64 t[2] = { }; |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 383 | if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) |
David Engraf | 9687fd9 | 2014-08-08 14:23:16 -0700 | [diff] [blame] | 384 | error("write error"); |
Christoph Hellwig | 38b0822 | 2020-07-30 08:19:00 +0200 | [diff] [blame] | 385 | |
| 386 | t[0].tv_sec = mtime; |
| 387 | t[1].tv_sec = mtime; |
| 388 | vfs_utimes(&wfile->f_path, t); |
| 389 | |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 390 | fput(wfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | eat(body_len); |
| 392 | state = SkipIt; |
| 393 | return 0; |
| 394 | } else { |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 395 | if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) |
David Engraf | 9687fd9 | 2014-08-08 14:23:16 -0700 | [diff] [blame] | 396 | error("write error"); |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 397 | body_len -= byte_count; |
| 398 | eat(byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return 1; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static int __init do_symlink(void) |
| 404 | { |
| 405 | collected[N_ALIGN(name_len) + body_len] = '\0'; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 406 | clean_path(collected, 0); |
Christoph Hellwig | cd3acb6 | 2020-07-22 11:14:36 +0200 | [diff] [blame] | 407 | init_symlink(collected + N_ALIGN(name_len), collected); |
Christoph Hellwig | b873498 | 2020-07-22 11:13:26 +0200 | [diff] [blame] | 408 | init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 409 | do_utime(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | state = SkipIt; |
| 411 | next_state = Reset; |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static __initdata int (*actions[])(void) = { |
| 416 | [Start] = do_start, |
| 417 | [Collect] = do_collect, |
| 418 | [GotHeader] = do_header, |
| 419 | [SkipIt] = do_skip, |
| 420 | [GotName] = do_name, |
| 421 | [CopyFile] = do_copy, |
| 422 | [GotSymlink] = do_symlink, |
| 423 | [Reset] = do_reset, |
| 424 | }; |
| 425 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 426 | static long __init write_buffer(char *buf, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 428 | byte_count = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | victim = buf; |
| 430 | |
| 431 | while (!actions[state]()) |
| 432 | ; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 433 | return len - byte_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 436 | static long __init flush_buffer(void *bufv, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 438 | char *buf = (char *) bufv; |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 439 | long written; |
| 440 | long origLen = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (message) |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 442 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | while ((written = write_buffer(buf, len)) < len && !message) { |
| 444 | char c = buf[written]; |
| 445 | if (c == '0') { |
| 446 | buf += written; |
| 447 | len -= written; |
| 448 | state = Start; |
| 449 | } else if (c == 0) { |
| 450 | buf += written; |
| 451 | len -= written; |
| 452 | state = Reset; |
| 453 | } else |
David Engraf | e5eed35 | 2019-03-07 16:30:19 -0800 | [diff] [blame] | 454 | error("junk within compressed archive"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 456 | return origLen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 459 | static unsigned long my_inptr; /* index of next byte to be processed in inbuf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 461 | #include <linux/decompress/generic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 463 | static char * __init unpack_to_rootfs(char *buf, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 465 | long written; |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 466 | decompress_fn decompress; |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 467 | const char *compress_name; |
| 468 | static __initdata char msg_buf[64]; |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 469 | |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 470 | header_buf = kmalloc(110, GFP_KERNEL); |
| 471 | symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); |
| 472 | name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 473 | |
| 474 | if (!header_buf || !symlink_buf || !name_buf) |
Florian Fainelli | dd23e80 | 2021-02-25 17:22:46 -0800 | [diff] [blame] | 475 | panic_show_mem("can't allocate buffers"); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | state = Start; |
| 478 | this_header = 0; |
| 479 | message = NULL; |
| 480 | while (!message && len) { |
| 481 | loff_t saved_offset = this_header; |
| 482 | if (*buf == '0' && !(this_header & 3)) { |
| 483 | state = Start; |
| 484 | written = write_buffer(buf, len); |
| 485 | buf += written; |
| 486 | len -= written; |
| 487 | continue; |
| 488 | } |
| 489 | if (!*buf) { |
| 490 | buf++; |
| 491 | len--; |
| 492 | this_header++; |
| 493 | continue; |
| 494 | } |
| 495 | this_header = 0; |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 496 | decompress = decompress_method(buf, len, &compress_name); |
Daniel M. Weeks | 6aa7a29 | 2014-04-07 15:39:16 -0700 | [diff] [blame] | 497 | pr_debug("Detected %s compressed data\n", compress_name); |
Phillip Lougher | 5429136 | 2009-12-14 21:45:19 +0000 | [diff] [blame] | 498 | if (decompress) { |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 499 | int res = decompress(buf, len, NULL, flush_buffer, NULL, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 500 | &my_inptr, error); |
Phillip Lougher | 5429136 | 2009-12-14 21:45:19 +0000 | [diff] [blame] | 501 | if (res) |
| 502 | error("decompressor failed"); |
| 503 | } else if (compress_name) { |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 504 | if (!message) { |
| 505 | snprintf(msg_buf, sizeof msg_buf, |
| 506 | "compression method %s not configured", |
| 507 | compress_name); |
| 508 | message = msg_buf; |
| 509 | } |
Phillip Lougher | df37bd1 | 2010-04-23 13:18:11 -0400 | [diff] [blame] | 510 | } else |
David Engraf | e5eed35 | 2019-03-07 16:30:19 -0800 | [diff] [blame] | 511 | error("invalid magic at start of compressed archive"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (state != Reset) |
David Engraf | e5eed35 | 2019-03-07 16:30:19 -0800 | [diff] [blame] | 513 | error("junk at the end of compressed archive"); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 514 | this_header = saved_offset + my_inptr; |
| 515 | buf += my_inptr; |
| 516 | len -= my_inptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 518 | dir_utime(); |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 519 | kfree(name_buf); |
| 520 | kfree(symlink_buf); |
| 521 | kfree(header_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | return message; |
| 523 | } |
| 524 | |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 525 | static int __initdata do_retain_initrd; |
| 526 | |
| 527 | static int __init retain_initrd_param(char *str) |
| 528 | { |
| 529 | if (*str) |
| 530 | return 0; |
| 531 | do_retain_initrd = 1; |
| 532 | return 1; |
| 533 | } |
| 534 | __setup("retain_initrd", retain_initrd_param); |
| 535 | |
Christoph Hellwig | d8ae8a3 | 2019-05-13 17:18:30 -0700 | [diff] [blame] | 536 | #ifdef CONFIG_ARCH_HAS_KEEPINITRD |
| 537 | static int __init keepinitrd_setup(char *__unused) |
| 538 | { |
| 539 | do_retain_initrd = 1; |
| 540 | return 1; |
| 541 | } |
| 542 | __setup("keepinitrd", keepinitrd_setup); |
| 543 | #endif |
| 544 | |
Rasmus Villemoes | e7cb072 | 2021-05-06 18:05:42 -0700 | [diff] [blame] | 545 | static bool __initdata initramfs_async = true; |
| 546 | static int __init initramfs_async_setup(char *str) |
| 547 | { |
| 548 | strtobool(str, &initramfs_async); |
| 549 | return 1; |
| 550 | } |
| 551 | __setup("initramfs_async=", initramfs_async_setup); |
| 552 | |
Hendrik Brueckner | ffe8018 | 2010-09-17 15:24:11 -0700 | [diff] [blame] | 553 | extern char __initramfs_start[]; |
| 554 | extern unsigned long __initramfs_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | #include <linux/initrd.h> |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 556 | #include <linux/kexec.h> |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 557 | |
Kefeng Wang | c72160f | 2021-01-15 13:46:04 +0800 | [diff] [blame] | 558 | void __init reserve_initrd_mem(void) |
| 559 | { |
| 560 | phys_addr_t start; |
| 561 | unsigned long size; |
| 562 | |
| 563 | /* Ignore the virtul address computed during device tree parsing */ |
| 564 | initrd_start = initrd_end = 0; |
| 565 | |
| 566 | if (!phys_initrd_size) |
| 567 | return; |
| 568 | /* |
| 569 | * Round the memory region to page boundaries as per free_initrd_mem() |
| 570 | * This allows us to detect whether the pages overlapping the initrd |
| 571 | * are in use, but more importantly, reserves the entire set of pages |
| 572 | * as we don't want these pages allocated for other purposes. |
| 573 | */ |
| 574 | start = round_down(phys_initrd_start, PAGE_SIZE); |
| 575 | size = phys_initrd_size + (phys_initrd_start - start); |
| 576 | size = round_up(size, PAGE_SIZE); |
| 577 | |
| 578 | if (!memblock_is_region_memory(start, size)) { |
| 579 | pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", |
| 580 | (u64)start, size); |
| 581 | goto disable; |
| 582 | } |
| 583 | |
| 584 | if (memblock_is_region_reserved(start, size)) { |
| 585 | pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", |
| 586 | (u64)start, size); |
| 587 | goto disable; |
| 588 | } |
| 589 | |
| 590 | memblock_reserve(start, size); |
| 591 | /* Now convert initrd to virtual addresses */ |
| 592 | initrd_start = (unsigned long)__va(phys_initrd_start); |
| 593 | initrd_end = initrd_start + phys_initrd_size; |
| 594 | initrd_below_start_ok = 1; |
| 595 | |
| 596 | return; |
| 597 | disable: |
| 598 | pr_cont(" - disabling initrd\n"); |
| 599 | initrd_start = 0; |
| 600 | initrd_end = 0; |
| 601 | } |
| 602 | |
Arnd Bergmann | 55d5b7dd | 2020-12-11 13:36:42 -0800 | [diff] [blame] | 603 | void __weak __init free_initrd_mem(unsigned long start, unsigned long end) |
Christoph Hellwig | 4afd58e | 2019-05-13 17:18:34 -0700 | [diff] [blame] | 604 | { |
Mike Rapoport | 899ee4a | 2019-09-28 11:02:26 +0300 | [diff] [blame] | 605 | #ifdef CONFIG_ARCH_KEEP_MEMBLOCK |
| 606 | unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE); |
| 607 | unsigned long aligned_end = ALIGN(end, PAGE_SIZE); |
| 608 | |
| 609 | memblock_free(__pa(aligned_start), aligned_end - aligned_start); |
| 610 | #endif |
| 611 | |
Christoph Hellwig | f94f743 | 2019-05-13 17:18:37 -0700 | [diff] [blame] | 612 | free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM, |
| 613 | "initrd"); |
Christoph Hellwig | 4afd58e | 2019-05-13 17:18:34 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 616 | #ifdef CONFIG_KEXEC_CORE |
Linus Torvalds | e99332e | 2020-05-09 17:50:03 -0700 | [diff] [blame] | 617 | static bool __init kexec_free_initrd(void) |
Christoph Hellwig | 23091e2 | 2019-05-13 17:18:21 -0700 | [diff] [blame] | 618 | { |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 619 | unsigned long crashk_start = (unsigned long)__va(crashk_res.start); |
| 620 | unsigned long crashk_end = (unsigned long)__va(crashk_res.end); |
| 621 | |
| 622 | /* |
| 623 | * If the initrd region is overlapped with crashkernel reserved region, |
| 624 | * free only memory that is not part of crashkernel region. |
| 625 | */ |
Christoph Hellwig | 23091e2 | 2019-05-13 17:18:21 -0700 | [diff] [blame] | 626 | if (initrd_start >= crashk_end || initrd_end <= crashk_start) |
| 627 | return false; |
| 628 | |
| 629 | /* |
| 630 | * Initialize initrd memory region since the kexec boot does not do. |
| 631 | */ |
| 632 | memset((void *)initrd_start, 0, initrd_end - initrd_start); |
| 633 | if (initrd_start < crashk_start) |
| 634 | free_initrd_mem(initrd_start, crashk_start); |
| 635 | if (initrd_end > crashk_end) |
| 636 | free_initrd_mem(crashk_end, initrd_end); |
| 637 | return true; |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 638 | } |
Christoph Hellwig | 23091e2 | 2019-05-13 17:18:21 -0700 | [diff] [blame] | 639 | #else |
| 640 | static inline bool kexec_free_initrd(void) |
| 641 | { |
| 642 | return false; |
| 643 | } |
| 644 | #endif /* CONFIG_KEXEC_CORE */ |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 645 | |
Andrew Morton | a841c67 | 2019-02-20 22:18:52 -0800 | [diff] [blame] | 646 | #ifdef CONFIG_BLK_DEV_RAM |
Geert Uytterhoeven | 4ada1e8 | 2019-06-28 12:07:03 -0700 | [diff] [blame] | 647 | static void __init populate_initrd_image(char *err) |
Christoph Hellwig | 7c184ec | 2019-05-13 17:18:24 -0700 | [diff] [blame] | 648 | { |
| 649 | ssize_t written; |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 650 | struct file *file; |
| 651 | loff_t pos = 0; |
Christoph Hellwig | 7c184ec | 2019-05-13 17:18:24 -0700 | [diff] [blame] | 652 | |
| 653 | unpack_to_rootfs(__initramfs_start, __initramfs_size); |
| 654 | |
| 655 | printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", |
| 656 | err); |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 657 | file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700); |
| 658 | if (IS_ERR(file)) |
Christoph Hellwig | 7c184ec | 2019-05-13 17:18:24 -0700 | [diff] [blame] | 659 | return; |
| 660 | |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 661 | written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start, |
| 662 | &pos); |
Christoph Hellwig | 7c184ec | 2019-05-13 17:18:24 -0700 | [diff] [blame] | 663 | if (written != initrd_end - initrd_start) |
| 664 | pr_err("/initrd.image: incomplete write (%zd != %ld)\n", |
| 665 | written, initrd_end - initrd_start); |
Christoph Hellwig | bf6419e | 2020-07-14 08:56:19 +0200 | [diff] [blame] | 666 | fput(file); |
Christoph Hellwig | 7c184ec | 2019-05-13 17:18:24 -0700 | [diff] [blame] | 667 | } |
| 668 | #endif /* CONFIG_BLK_DEV_RAM */ |
| 669 | |
Rasmus Villemoes | e7cb072 | 2021-05-06 18:05:42 -0700 | [diff] [blame] | 670 | static void __init do_populate_rootfs(void *unused, async_cookie_t cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | { |
Stafford Horne | 17a9be3 | 2017-05-04 21:15:56 +0900 | [diff] [blame] | 672 | /* Load the built in initramfs */ |
Hendrik Brueckner | ffe8018 | 2010-09-17 15:24:11 -0700 | [diff] [blame] | 673 | char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (err) |
Florian Fainelli | dd23e80 | 2021-02-25 17:22:46 -0800 | [diff] [blame] | 675 | panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */ |
Christoph Hellwig | 54c7a89 | 2019-05-13 17:18:17 -0700 | [diff] [blame] | 676 | |
Christoph Hellwig | afef788 | 2019-05-13 17:18:27 -0700 | [diff] [blame] | 677 | if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE)) |
| 678 | goto done; |
| 679 | |
| 680 | if (IS_ENABLED(CONFIG_BLK_DEV_RAM)) |
| 681 | printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); |
| 682 | else |
| 683 | printk(KERN_INFO "Unpacking initramfs...\n"); |
| 684 | |
| 685 | err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start); |
| 686 | if (err) { |
Christoph Hellwig | 9ab6b71 | 2020-06-06 13:51:52 +0200 | [diff] [blame] | 687 | #ifdef CONFIG_BLK_DEV_RAM |
Christoph Hellwig | 7c184ec | 2019-05-13 17:18:24 -0700 | [diff] [blame] | 688 | populate_initrd_image(err); |
Christoph Hellwig | 9ab6b71 | 2020-06-06 13:51:52 +0200 | [diff] [blame] | 689 | #else |
| 690 | printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); |
| 691 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
Christoph Hellwig | 23091e2 | 2019-05-13 17:18:21 -0700 | [diff] [blame] | 693 | |
Christoph Hellwig | afef788 | 2019-05-13 17:18:27 -0700 | [diff] [blame] | 694 | done: |
Christoph Hellwig | 23091e2 | 2019-05-13 17:18:21 -0700 | [diff] [blame] | 695 | /* |
| 696 | * If the initrd region is overlapped with crashkernel reserved region, |
| 697 | * free only memory that is not part of crashkernel region. |
| 698 | */ |
Steven Price | 5d59aa8 | 2019-05-17 14:31:47 -0700 | [diff] [blame] | 699 | if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) |
Christoph Hellwig | 23091e2 | 2019-05-13 17:18:21 -0700 | [diff] [blame] | 700 | free_initrd_mem(initrd_start, initrd_end); |
| 701 | initrd_start = 0; |
| 702 | initrd_end = 0; |
| 703 | |
Stafford Horne | 17a9be3 | 2017-05-04 21:15:56 +0900 | [diff] [blame] | 704 | flush_delayed_fput(); |
Rasmus Villemoes | e7cb072 | 2021-05-06 18:05:42 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain); |
| 708 | static async_cookie_t initramfs_cookie; |
| 709 | |
| 710 | void wait_for_initramfs(void) |
| 711 | { |
| 712 | if (!initramfs_cookie) { |
| 713 | /* |
| 714 | * Something before rootfs_initcall wants to access |
| 715 | * the filesystem/initramfs. Probably a bug. Make a |
| 716 | * note, avoid deadlocking the machine, and let the |
| 717 | * caller's access fail as it used to. |
| 718 | */ |
| 719 | pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n"); |
| 720 | return; |
| 721 | } |
| 722 | async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain); |
| 723 | } |
| 724 | EXPORT_SYMBOL_GPL(wait_for_initramfs); |
| 725 | |
| 726 | static int __init populate_rootfs(void) |
| 727 | { |
| 728 | initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL, |
| 729 | &initramfs_domain); |
| 730 | if (!initramfs_async) |
| 731 | wait_for_initramfs(); |
Linus Torvalds | 8d610dd | 2006-12-11 12:12:04 -0800 | [diff] [blame] | 732 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
Linus Torvalds | 8d610dd | 2006-12-11 12:12:04 -0800 | [diff] [blame] | 734 | rootfs_initcall(populate_rootfs); |