H Hartley Sweeten | c67e538 | 2012-05-31 16:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Many of the syscalls used in this file expect some of the arguments |
| 3 | * to be __user pointers not __kernel pointers. To limit the sparse |
| 4 | * noise, turn off sparse checking for this file. |
| 5 | */ |
| 6 | #ifdef __CHECKER__ |
| 7 | #undef __CHECKER__ |
| 8 | #warning "Sparse checking disabled for this file" |
| 9 | #endif |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/fcntl.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/string.h> |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 18 | #include <linux/dirent.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/syscalls.h> |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 20 | #include <linux/utime.h> |
Lokesh Vutla | 0886551 | 2017-02-27 14:28:12 -0800 | [diff] [blame] | 21 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Yinghai Lu | 3874743 | 2014-08-08 14:23:12 -0700 | [diff] [blame] | 23 | static ssize_t __init xwrite(int fd, const char *p, size_t count) |
| 24 | { |
| 25 | ssize_t out = 0; |
| 26 | |
| 27 | /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ |
| 28 | while (count) { |
| 29 | ssize_t rv = sys_write(fd, p, count); |
| 30 | |
| 31 | if (rv < 0) { |
| 32 | if (rv == -EINTR || rv == -EAGAIN) |
| 33 | continue; |
| 34 | return out ? out : rv; |
| 35 | } else if (rv == 0) |
| 36 | break; |
| 37 | |
| 38 | p += rv; |
| 39 | out += rv; |
| 40 | count -= rv; |
| 41 | } |
| 42 | |
| 43 | return out; |
| 44 | } |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static __initdata char *message; |
| 47 | static void __init error(char *x) |
| 48 | { |
| 49 | if (!message) |
| 50 | message = x; |
| 51 | } |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* link hash */ |
| 54 | |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 55 | #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static __initdata struct hash { |
| 58 | int ino, minor, major; |
Al Viro | 685dd2d | 2011-07-26 04:34:13 -0400 | [diff] [blame] | 59 | umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct hash *next; |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 61 | char name[N_ALIGN(PATH_MAX)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } *head[32]; |
| 63 | |
| 64 | static inline int hash(int major, int minor, int ino) |
| 65 | { |
| 66 | unsigned long tmp = ino + minor + (major << 3); |
| 67 | tmp += tmp >> 5; |
| 68 | return tmp & 31; |
| 69 | } |
| 70 | |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 71 | static char __init *find_link(int major, int minor, int ino, |
Al Viro | 685dd2d | 2011-07-26 04:34:13 -0400 | [diff] [blame] | 72 | umode_t mode, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | struct hash **p, *q; |
| 75 | for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { |
| 76 | if ((*p)->ino != ino) |
| 77 | continue; |
| 78 | if ((*p)->minor != minor) |
| 79 | continue; |
| 80 | if ((*p)->major != major) |
| 81 | continue; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 82 | if (((*p)->mode ^ mode) & S_IFMT) |
| 83 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return (*p)->name; |
| 85 | } |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 86 | q = kmalloc(sizeof(struct hash), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (!q) |
| 88 | panic("can't allocate link hash entry"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | q->major = major; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 90 | q->minor = minor; |
| 91 | q->ino = ino; |
| 92 | q->mode = mode; |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 93 | strcpy(q->name, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | q->next = NULL; |
| 95 | *p = q; |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | static void __init free_hash(void) |
| 100 | { |
| 101 | struct hash **p, *q; |
| 102 | for (p = head; p < head + 32; p++) { |
| 103 | while (*p) { |
| 104 | q = *p; |
| 105 | *p = q->next; |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 106 | kfree(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
H Hartley Sweeten | c67e538 | 2012-05-31 16:26:10 -0700 | [diff] [blame] | 111 | static long __init do_utime(char *filename, time_t mtime) |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 112 | { |
| 113 | struct timespec t[2]; |
| 114 | |
| 115 | t[0].tv_sec = mtime; |
| 116 | t[0].tv_nsec = 0; |
| 117 | t[1].tv_sec = mtime; |
| 118 | t[1].tv_nsec = 0; |
| 119 | |
| 120 | return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW); |
| 121 | } |
| 122 | |
| 123 | static __initdata LIST_HEAD(dir_list); |
| 124 | struct dir_entry { |
| 125 | struct list_head list; |
| 126 | char *name; |
| 127 | time_t mtime; |
| 128 | }; |
| 129 | |
| 130 | static void __init dir_add(const char *name, time_t mtime) |
| 131 | { |
| 132 | struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL); |
| 133 | if (!de) |
| 134 | panic("can't allocate dir_entry buffer"); |
| 135 | INIT_LIST_HEAD(&de->list); |
| 136 | de->name = kstrdup(name, GFP_KERNEL); |
| 137 | de->mtime = mtime; |
| 138 | list_add(&de->list, &dir_list); |
| 139 | } |
| 140 | |
| 141 | static void __init dir_utime(void) |
| 142 | { |
| 143 | struct dir_entry *de, *tmp; |
| 144 | list_for_each_entry_safe(de, tmp, &dir_list, list) { |
| 145 | list_del(&de->list); |
| 146 | do_utime(de->name, de->mtime); |
| 147 | kfree(de->name); |
| 148 | kfree(de); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static __initdata time_t mtime; |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* cpio header parsing */ |
| 155 | |
| 156 | static __initdata unsigned long ino, major, minor, nlink; |
Al Viro | 685dd2d | 2011-07-26 04:34:13 -0400 | [diff] [blame] | 157 | static __initdata umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | static __initdata unsigned long body_len, name_len; |
| 159 | static __initdata uid_t uid; |
| 160 | static __initdata gid_t gid; |
| 161 | static __initdata unsigned rdev; |
| 162 | |
| 163 | static void __init parse_header(char *s) |
| 164 | { |
| 165 | unsigned long parsed[12]; |
| 166 | char buf[9]; |
| 167 | int i; |
| 168 | |
| 169 | buf[8] = '\0'; |
| 170 | for (i = 0, s += 6; i < 12; i++, s += 8) { |
| 171 | memcpy(buf, s, 8); |
| 172 | parsed[i] = simple_strtoul(buf, NULL, 16); |
| 173 | } |
| 174 | ino = parsed[0]; |
| 175 | mode = parsed[1]; |
| 176 | uid = parsed[2]; |
| 177 | gid = parsed[3]; |
| 178 | nlink = parsed[4]; |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 179 | mtime = parsed[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | body_len = parsed[6]; |
| 181 | major = parsed[7]; |
| 182 | minor = parsed[8]; |
| 183 | rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); |
| 184 | name_len = parsed[11]; |
| 185 | } |
| 186 | |
| 187 | /* FSM */ |
| 188 | |
| 189 | static __initdata enum state { |
| 190 | Start, |
| 191 | Collect, |
| 192 | GotHeader, |
| 193 | SkipIt, |
| 194 | GotName, |
| 195 | CopyFile, |
| 196 | GotSymlink, |
| 197 | Reset |
| 198 | } state, next_state; |
| 199 | |
| 200 | static __initdata char *victim; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 201 | static unsigned long byte_count __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | static __initdata loff_t this_header, next_header; |
| 203 | |
Al Viro | b0a5ab9 | 2007-07-26 17:33:59 +0100 | [diff] [blame] | 204 | static inline void __init eat(unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | victim += n; |
| 207 | this_header += n; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 208 | byte_count -= n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 211 | static __initdata char *vcollected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | static __initdata char *collected; |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 213 | static long remains __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | static __initdata char *collect; |
| 215 | |
| 216 | static void __init read_into(char *buf, unsigned size, enum state next) |
| 217 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 218 | if (byte_count >= size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | collected = victim; |
| 220 | eat(size); |
| 221 | state = next; |
| 222 | } else { |
| 223 | collect = collected = buf; |
| 224 | remains = size; |
| 225 | next_state = next; |
| 226 | state = Collect; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static __initdata char *header_buf, *symlink_buf, *name_buf; |
| 231 | |
| 232 | static int __init do_start(void) |
| 233 | { |
| 234 | read_into(header_buf, 110, GotHeader); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int __init do_collect(void) |
| 239 | { |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 240 | unsigned long n = remains; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 241 | if (byte_count < n) |
| 242 | n = byte_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | memcpy(collect, victim, n); |
| 244 | eat(n); |
| 245 | collect += n; |
| 246 | if ((remains -= n) != 0) |
| 247 | return 1; |
| 248 | state = next_state; |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int __init do_header(void) |
| 253 | { |
Arjan van de Ven | 2e591bb | 2006-12-06 20:37:19 -0800 | [diff] [blame] | 254 | if (memcmp(collected, "070707", 6)==0) { |
| 255 | error("incorrect cpio method used: use -H newc option"); |
| 256 | return 1; |
| 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (memcmp(collected, "070701", 6)) { |
| 259 | error("no cpio magic"); |
| 260 | return 1; |
| 261 | } |
| 262 | parse_header(collected); |
| 263 | next_header = this_header + N_ALIGN(name_len) + body_len; |
| 264 | next_header = (next_header + 3) & ~3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | state = SkipIt; |
| 266 | if (name_len <= 0 || name_len > PATH_MAX) |
| 267 | return 0; |
| 268 | if (S_ISLNK(mode)) { |
| 269 | if (body_len > PATH_MAX) |
| 270 | return 0; |
| 271 | collect = collected = symlink_buf; |
| 272 | remains = N_ALIGN(name_len) + body_len; |
| 273 | next_state = GotSymlink; |
| 274 | state = Collect; |
| 275 | return 0; |
| 276 | } |
| 277 | if (S_ISREG(mode) || !body_len) |
| 278 | read_into(name_buf, N_ALIGN(name_len), GotName); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int __init do_skip(void) |
| 283 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 284 | if (this_header + byte_count < next_header) { |
| 285 | eat(byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return 1; |
| 287 | } else { |
| 288 | eat(next_header - this_header); |
| 289 | state = next_state; |
| 290 | return 0; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static int __init do_reset(void) |
| 295 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 296 | while (byte_count && *victim == '\0') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | eat(1); |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 298 | if (byte_count && (this_header & 3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | error("broken padding"); |
| 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | static int __init maybe_link(void) |
| 304 | { |
| 305 | if (nlink >= 2) { |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 306 | char *old = find_link(major, minor, ino, mode, collected); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (old) |
| 308 | return (sys_link(old, collected) < 0) ? -1 : 1; |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 313 | static void __init clean_path(char *path, umode_t fmode) |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 314 | { |
| 315 | struct stat st; |
| 316 | |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 317 | if (!sys_newlstat(path, &st) && (st.st_mode ^ fmode) & S_IFMT) { |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 318 | if (S_ISDIR(st.st_mode)) |
| 319 | sys_rmdir(path); |
| 320 | else |
| 321 | sys_unlink(path); |
| 322 | } |
| 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | static __initdata int wfd; |
| 326 | |
| 327 | static int __init do_name(void) |
| 328 | { |
| 329 | state = SkipIt; |
| 330 | next_state = Reset; |
| 331 | if (strcmp(collected, "TRAILER!!!") == 0) { |
| 332 | free_hash(); |
| 333 | return 0; |
| 334 | } |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 335 | clean_path(collected, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (S_ISREG(mode)) { |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 337 | int ml = maybe_link(); |
| 338 | if (ml >= 0) { |
| 339 | int openflags = O_WRONLY|O_CREAT; |
| 340 | if (ml != 1) |
| 341 | openflags |= O_TRUNC; |
| 342 | wfd = sys_open(collected, openflags, mode); |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (wfd >= 0) { |
| 345 | sys_fchown(wfd, uid, gid); |
| 346 | sys_fchmod(wfd, mode); |
Randy Robertson | d20d5a7 | 2009-04-13 14:40:04 -0700 | [diff] [blame] | 347 | if (body_len) |
| 348 | sys_ftruncate(wfd, body_len); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 349 | vcollected = kstrdup(collected, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | state = CopyFile; |
| 351 | } |
| 352 | } |
| 353 | } else if (S_ISDIR(mode)) { |
| 354 | sys_mkdir(collected, mode); |
| 355 | sys_chown(collected, uid, gid); |
| 356 | sys_chmod(collected, mode); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 357 | dir_add(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } else if (S_ISBLK(mode) || S_ISCHR(mode) || |
| 359 | S_ISFIFO(mode) || S_ISSOCK(mode)) { |
| 360 | if (maybe_link() == 0) { |
| 361 | sys_mknod(collected, mode, rdev); |
| 362 | sys_chown(collected, uid, gid); |
| 363 | sys_chmod(collected, mode); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 364 | do_utime(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int __init do_copy(void) |
| 371 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 372 | if (byte_count >= body_len) { |
David Engraf | 9687fd9 | 2014-08-08 14:23:16 -0700 | [diff] [blame] | 373 | if (xwrite(wfd, victim, body_len) != body_len) |
| 374 | error("write error"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | sys_close(wfd); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 376 | do_utime(vcollected, mtime); |
| 377 | kfree(vcollected); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | eat(body_len); |
| 379 | state = SkipIt; |
| 380 | return 0; |
| 381 | } else { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 382 | if (xwrite(wfd, victim, byte_count) != byte_count) |
David Engraf | 9687fd9 | 2014-08-08 14:23:16 -0700 | [diff] [blame] | 383 | error("write error"); |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 384 | body_len -= byte_count; |
| 385 | eat(byte_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return 1; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static int __init do_symlink(void) |
| 391 | { |
| 392 | collected[N_ALIGN(name_len) + body_len] = '\0'; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 393 | clean_path(collected, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | sys_symlink(collected + N_ALIGN(name_len), collected); |
| 395 | sys_lchown(collected, uid, gid); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 396 | do_utime(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | state = SkipIt; |
| 398 | next_state = Reset; |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static __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 Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 413 | static long __init write_buffer(char *buf, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 415 | byte_count = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | victim = buf; |
| 417 | |
| 418 | while (!actions[state]()) |
| 419 | ; |
Mark Rustad | c34d85a | 2014-10-13 15:54:07 -0700 | [diff] [blame] | 420 | return len - byte_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 423 | static long __init flush_buffer(void *bufv, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 425 | char *buf = (char *) bufv; |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 426 | long written; |
| 427 | long origLen = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | if (message) |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 429 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | 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 |
| 441 | error("junk in compressed archive"); |
| 442 | } |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 443 | return origLen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 446 | 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] | 447 | |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 448 | #include <linux/decompress/generic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 450 | static char * __init unpack_to_rootfs(char *buf, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 452 | long written; |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 453 | decompress_fn decompress; |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 454 | const char *compress_name; |
| 455 | static __initdata char msg_buf[64]; |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 456 | |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 457 | 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 Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 460 | |
| 461 | if (!header_buf || !symlink_buf || !name_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | panic("can't allocate buffers"); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | 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 Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 483 | decompress = decompress_method(buf, len, &compress_name); |
Daniel M. Weeks | 6aa7a29 | 2014-04-07 15:39:16 -0700 | [diff] [blame] | 484 | pr_debug("Detected %s compressed data\n", compress_name); |
Phillip Lougher | 5429136 | 2009-12-14 21:45:19 +0000 | [diff] [blame] | 485 | if (decompress) { |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 486 | int res = decompress(buf, len, NULL, flush_buffer, NULL, |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 487 | &my_inptr, error); |
Phillip Lougher | 5429136 | 2009-12-14 21:45:19 +0000 | [diff] [blame] | 488 | if (res) |
| 489 | error("decompressor failed"); |
| 490 | } else if (compress_name) { |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 491 | if (!message) { |
| 492 | snprintf(msg_buf, sizeof msg_buf, |
| 493 | "compression method %s not configured", |
| 494 | compress_name); |
| 495 | message = msg_buf; |
| 496 | } |
Phillip Lougher | df37bd1 | 2010-04-23 13:18:11 -0400 | [diff] [blame] | 497 | } else |
| 498 | error("junk in compressed archive"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | if (state != Reset) |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 500 | error("junk in compressed archive"); |
| 501 | this_header = saved_offset + my_inptr; |
| 502 | buf += my_inptr; |
| 503 | len -= my_inptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 505 | dir_utime(); |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 506 | kfree(name_buf); |
| 507 | kfree(symlink_buf); |
| 508 | kfree(header_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return message; |
| 510 | } |
| 511 | |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 512 | static int __initdata do_retain_initrd; |
| 513 | |
| 514 | static 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 | |
Hendrik Brueckner | ffe8018 | 2010-09-17 15:24:11 -0700 | [diff] [blame] | 523 | extern char __initramfs_start[]; |
| 524 | extern unsigned long __initramfs_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | #include <linux/initrd.h> |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 526 | #include <linux/kexec.h> |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 527 | |
| 528 | static void __init free_initrd(void) |
| 529 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 530 | #ifdef CONFIG_KEXEC_CORE |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 531 | unsigned long crashk_start = (unsigned long)__va(crashk_res.start); |
| 532 | unsigned long crashk_end = (unsigned long)__va(crashk_res.end); |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 533 | #endif |
| 534 | if (do_retain_initrd) |
| 535 | goto skip; |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 536 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 537 | #ifdef CONFIG_KEXEC_CORE |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 538 | /* |
| 539 | * If the initrd region is overlapped with crashkernel reserved region, |
| 540 | * free only memory that is not part of crashkernel region. |
| 541 | */ |
| 542 | if (initrd_start < crashk_end && initrd_end > crashk_start) { |
| 543 | /* |
| 544 | * Initialize initrd memory region since the kexec boot does |
| 545 | * not do. |
| 546 | */ |
| 547 | memset((void *)initrd_start, 0, initrd_end - initrd_start); |
| 548 | if (initrd_start < crashk_start) |
| 549 | free_initrd_mem(initrd_start, crashk_start); |
| 550 | if (initrd_end > crashk_end) |
| 551 | free_initrd_mem(crashk_end, initrd_end); |
| 552 | } else |
| 553 | #endif |
| 554 | free_initrd_mem(initrd_start, initrd_end); |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 555 | skip: |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 556 | initrd_start = 0; |
| 557 | initrd_end = 0; |
| 558 | } |
| 559 | |
Nikanth Karthikesan | b52bb37 | 2009-04-13 14:39:38 -0700 | [diff] [blame] | 560 | #ifdef CONFIG_BLK_DEV_RAM |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 561 | #define BUF_SIZE 1024 |
| 562 | static void __init clean_rootfs(void) |
| 563 | { |
| 564 | int fd; |
| 565 | void *buf; |
| 566 | struct linux_dirent64 *dirp; |
H Hartley Sweeten | 8aaed5b | 2010-03-05 13:42:39 -0800 | [diff] [blame] | 567 | int num; |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 568 | |
H Hartley Sweeten | c67e538 | 2012-05-31 16:26:10 -0700 | [diff] [blame] | 569 | fd = sys_open("/", O_RDONLY, 0); |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 570 | WARN_ON(fd < 0); |
| 571 | if (fd < 0) |
| 572 | return; |
| 573 | buf = kzalloc(BUF_SIZE, GFP_KERNEL); |
| 574 | WARN_ON(!buf); |
| 575 | if (!buf) { |
| 576 | sys_close(fd); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | dirp = buf; |
H Hartley Sweeten | 8aaed5b | 2010-03-05 13:42:39 -0800 | [diff] [blame] | 581 | num = sys_getdents64(fd, dirp, BUF_SIZE); |
| 582 | while (num > 0) { |
| 583 | while (num > 0) { |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 584 | struct stat st; |
| 585 | int ret; |
| 586 | |
| 587 | ret = sys_newlstat(dirp->d_name, &st); |
| 588 | WARN_ON_ONCE(ret); |
| 589 | if (!ret) { |
| 590 | if (S_ISDIR(st.st_mode)) |
| 591 | sys_rmdir(dirp->d_name); |
| 592 | else |
| 593 | sys_unlink(dirp->d_name); |
| 594 | } |
| 595 | |
H Hartley Sweeten | 8aaed5b | 2010-03-05 13:42:39 -0800 | [diff] [blame] | 596 | num -= dirp->d_reclen; |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 597 | dirp = (void *)dirp + dirp->d_reclen; |
| 598 | } |
| 599 | dirp = buf; |
| 600 | memset(buf, 0, BUF_SIZE); |
H Hartley Sweeten | 8aaed5b | 2010-03-05 13:42:39 -0800 | [diff] [blame] | 601 | num = sys_getdents64(fd, dirp, BUF_SIZE); |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | sys_close(fd); |
| 605 | kfree(buf); |
| 606 | } |
Nikanth Karthikesan | b52bb37 | 2009-04-13 14:39:38 -0700 | [diff] [blame] | 607 | #endif |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 608 | |
Linus Torvalds | 9a9e0d6 | 2008-03-15 11:53:32 -0700 | [diff] [blame] | 609 | static int __init populate_rootfs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Hendrik Brueckner | ffe8018 | 2010-09-17 15:24:11 -0700 | [diff] [blame] | 611 | char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if (err) |
Tetsuo Handa | 499a458 | 2014-01-23 15:54:56 -0800 | [diff] [blame] | 613 | panic("%s", err); /* Failed to decompress INTERNAL initramfs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | if (initrd_start) { |
Zdenek Pavlas | 340e48e | 2006-03-25 03:07:49 -0800 | [diff] [blame] | 615 | #ifdef CONFIG_BLK_DEV_RAM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | int fd; |
Eric Piel | a1e6b6c | 2009-05-06 16:03:06 -0700 | [diff] [blame] | 617 | printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | err = unpack_to_rootfs((char *)initrd_start, |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 619 | initrd_end - initrd_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | if (!err) { |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 621 | free_initrd(); |
Tejun Heo | bb813f4 | 2013-01-18 14:05:56 -0800 | [diff] [blame] | 622 | goto done; |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 623 | } else { |
| 624 | clean_rootfs(); |
Hendrik Brueckner | ffe8018 | 2010-09-17 15:24:11 -0700 | [diff] [blame] | 625 | unpack_to_rootfs(__initramfs_start, __initramfs_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
Simon Kitching | c1c490e | 2009-04-02 16:57:00 -0700 | [diff] [blame] | 627 | printk(KERN_INFO "rootfs image is not initramfs (%s)" |
| 628 | "; looks like an initrd\n", err); |
H Hartley Sweeten | c67e538 | 2012-05-31 16:26:10 -0700 | [diff] [blame] | 629 | fd = sys_open("/initrd.image", |
Namhyung Kim | 562f5e6 | 2010-10-26 14:22:42 -0700 | [diff] [blame] | 630 | O_WRONLY|O_CREAT, 0700); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (fd >= 0) { |
Yinghai Lu | 3874743 | 2014-08-08 14:23:12 -0700 | [diff] [blame] | 632 | ssize_t written = xwrite(fd, (char *)initrd_start, |
| 633 | initrd_end - initrd_start); |
| 634 | |
| 635 | if (written != initrd_end - initrd_start) |
| 636 | pr_err("/initrd.image: incomplete write (%zd != %ld)\n", |
| 637 | written, initrd_end - initrd_start); |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | sys_close(fd); |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 640 | free_initrd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Tejun Heo | bb813f4 | 2013-01-18 14:05:56 -0800 | [diff] [blame] | 642 | done: |
Zdenek Pavlas | 340e48e | 2006-03-25 03:07:49 -0800 | [diff] [blame] | 643 | #else |
Eric Piel | a1e6b6c | 2009-05-06 16:03:06 -0700 | [diff] [blame] | 644 | printk(KERN_INFO "Unpacking initramfs...\n"); |
Zdenek Pavlas | 340e48e | 2006-03-25 03:07:49 -0800 | [diff] [blame] | 645 | err = unpack_to_rootfs((char *)initrd_start, |
Li, Shaohua | df52092 | 2008-08-13 17:26:01 +0800 | [diff] [blame] | 646 | initrd_end - initrd_start); |
Eric Piel | a1e6b6c | 2009-05-06 16:03:06 -0700 | [diff] [blame] | 647 | if (err) |
| 648 | printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); |
Zdenek Pavlas | 340e48e | 2006-03-25 03:07:49 -0800 | [diff] [blame] | 649 | free_initrd(); |
| 650 | #endif |
Lokesh Vutla | 0886551 | 2017-02-27 14:28:12 -0800 | [diff] [blame] | 651 | flush_delayed_fput(); |
Tejun Heo | bb813f4 | 2013-01-18 14:05:56 -0800 | [diff] [blame] | 652 | /* |
| 653 | * Try loading default modules from initramfs. This gives |
| 654 | * us a chance to load before device_initcalls. |
| 655 | */ |
| 656 | load_default_modules(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
Linus Torvalds | 8d610dd | 2006-12-11 12:12:04 -0800 | [diff] [blame] | 658 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
Linus Torvalds | 8d610dd | 2006-12-11 12:12:04 -0800 | [diff] [blame] | 660 | rootfs_initcall(populate_rootfs); |