Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/init.h> |
| 2 | #include <linux/fs.h> |
| 3 | #include <linux/slab.h> |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/fcntl.h> |
| 6 | #include <linux/delay.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/syscalls.h> |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 9 | #include <linux/utime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | static __initdata char *message; |
| 12 | static void __init error(char *x) |
| 13 | { |
| 14 | if (!message) |
| 15 | message = x; |
| 16 | } |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* link hash */ |
| 19 | |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 20 | #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | static __initdata struct hash { |
| 23 | int ino, minor, major; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 24 | mode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | struct hash *next; |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 26 | char name[N_ALIGN(PATH_MAX)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } *head[32]; |
| 28 | |
| 29 | static inline int hash(int major, int minor, int ino) |
| 30 | { |
| 31 | unsigned long tmp = ino + minor + (major << 3); |
| 32 | tmp += tmp >> 5; |
| 33 | return tmp & 31; |
| 34 | } |
| 35 | |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 36 | static char __init *find_link(int major, int minor, int ino, |
| 37 | mode_t mode, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | struct hash **p, *q; |
| 40 | for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { |
| 41 | if ((*p)->ino != ino) |
| 42 | continue; |
| 43 | if ((*p)->minor != minor) |
| 44 | continue; |
| 45 | if ((*p)->major != major) |
| 46 | continue; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 47 | if (((*p)->mode ^ mode) & S_IFMT) |
| 48 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | return (*p)->name; |
| 50 | } |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 51 | q = kmalloc(sizeof(struct hash), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (!q) |
| 53 | panic("can't allocate link hash entry"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | q->major = major; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 55 | q->minor = minor; |
| 56 | q->ino = ino; |
| 57 | q->mode = mode; |
Mark Huang | 6a050da | 2006-05-15 09:44:03 -0700 | [diff] [blame] | 58 | strcpy(q->name, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | q->next = NULL; |
| 60 | *p = q; |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static void __init free_hash(void) |
| 65 | { |
| 66 | struct hash **p, *q; |
| 67 | for (p = head; p < head + 32; p++) { |
| 68 | while (*p) { |
| 69 | q = *p; |
| 70 | *p = q->next; |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 71 | kfree(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 76 | static long __init do_utime(char __user *filename, time_t mtime) |
| 77 | { |
| 78 | struct timespec t[2]; |
| 79 | |
| 80 | t[0].tv_sec = mtime; |
| 81 | t[0].tv_nsec = 0; |
| 82 | t[1].tv_sec = mtime; |
| 83 | t[1].tv_nsec = 0; |
| 84 | |
| 85 | return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW); |
| 86 | } |
| 87 | |
| 88 | static __initdata LIST_HEAD(dir_list); |
| 89 | struct dir_entry { |
| 90 | struct list_head list; |
| 91 | char *name; |
| 92 | time_t mtime; |
| 93 | }; |
| 94 | |
| 95 | static void __init dir_add(const char *name, time_t mtime) |
| 96 | { |
| 97 | struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL); |
| 98 | if (!de) |
| 99 | panic("can't allocate dir_entry buffer"); |
| 100 | INIT_LIST_HEAD(&de->list); |
| 101 | de->name = kstrdup(name, GFP_KERNEL); |
| 102 | de->mtime = mtime; |
| 103 | list_add(&de->list, &dir_list); |
| 104 | } |
| 105 | |
| 106 | static void __init dir_utime(void) |
| 107 | { |
| 108 | struct dir_entry *de, *tmp; |
| 109 | list_for_each_entry_safe(de, tmp, &dir_list, list) { |
| 110 | list_del(&de->list); |
| 111 | do_utime(de->name, de->mtime); |
| 112 | kfree(de->name); |
| 113 | kfree(de); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static __initdata time_t mtime; |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* cpio header parsing */ |
| 120 | |
| 121 | static __initdata unsigned long ino, major, minor, nlink; |
| 122 | static __initdata mode_t mode; |
| 123 | static __initdata unsigned long body_len, name_len; |
| 124 | static __initdata uid_t uid; |
| 125 | static __initdata gid_t gid; |
| 126 | static __initdata unsigned rdev; |
| 127 | |
| 128 | static void __init parse_header(char *s) |
| 129 | { |
| 130 | unsigned long parsed[12]; |
| 131 | char buf[9]; |
| 132 | int i; |
| 133 | |
| 134 | buf[8] = '\0'; |
| 135 | for (i = 0, s += 6; i < 12; i++, s += 8) { |
| 136 | memcpy(buf, s, 8); |
| 137 | parsed[i] = simple_strtoul(buf, NULL, 16); |
| 138 | } |
| 139 | ino = parsed[0]; |
| 140 | mode = parsed[1]; |
| 141 | uid = parsed[2]; |
| 142 | gid = parsed[3]; |
| 143 | nlink = parsed[4]; |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 144 | mtime = parsed[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | body_len = parsed[6]; |
| 146 | major = parsed[7]; |
| 147 | minor = parsed[8]; |
| 148 | rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); |
| 149 | name_len = parsed[11]; |
| 150 | } |
| 151 | |
| 152 | /* FSM */ |
| 153 | |
| 154 | static __initdata enum state { |
| 155 | Start, |
| 156 | Collect, |
| 157 | GotHeader, |
| 158 | SkipIt, |
| 159 | GotName, |
| 160 | CopyFile, |
| 161 | GotSymlink, |
| 162 | Reset |
| 163 | } state, next_state; |
| 164 | |
| 165 | static __initdata char *victim; |
| 166 | static __initdata unsigned count; |
| 167 | static __initdata loff_t this_header, next_header; |
| 168 | |
| 169 | static __initdata int dry_run; |
| 170 | |
Al Viro | b0a5ab9 | 2007-07-26 17:33:59 +0100 | [diff] [blame] | 171 | static inline void __init eat(unsigned n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | victim += n; |
| 174 | this_header += n; |
| 175 | count -= n; |
| 176 | } |
| 177 | |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 178 | static __initdata char *vcollected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | static __initdata char *collected; |
| 180 | static __initdata int remains; |
| 181 | static __initdata char *collect; |
| 182 | |
| 183 | static void __init read_into(char *buf, unsigned size, enum state next) |
| 184 | { |
| 185 | if (count >= size) { |
| 186 | collected = victim; |
| 187 | eat(size); |
| 188 | state = next; |
| 189 | } else { |
| 190 | collect = collected = buf; |
| 191 | remains = size; |
| 192 | next_state = next; |
| 193 | state = Collect; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static __initdata char *header_buf, *symlink_buf, *name_buf; |
| 198 | |
| 199 | static int __init do_start(void) |
| 200 | { |
| 201 | read_into(header_buf, 110, GotHeader); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int __init do_collect(void) |
| 206 | { |
| 207 | unsigned n = remains; |
| 208 | if (count < n) |
| 209 | n = count; |
| 210 | memcpy(collect, victim, n); |
| 211 | eat(n); |
| 212 | collect += n; |
| 213 | if ((remains -= n) != 0) |
| 214 | return 1; |
| 215 | state = next_state; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int __init do_header(void) |
| 220 | { |
Arjan van de Ven | 2e591bb | 2006-12-06 20:37:19 -0800 | [diff] [blame] | 221 | if (memcmp(collected, "070707", 6)==0) { |
| 222 | error("incorrect cpio method used: use -H newc option"); |
| 223 | return 1; |
| 224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | if (memcmp(collected, "070701", 6)) { |
| 226 | error("no cpio magic"); |
| 227 | return 1; |
| 228 | } |
| 229 | parse_header(collected); |
| 230 | next_header = this_header + N_ALIGN(name_len) + body_len; |
| 231 | next_header = (next_header + 3) & ~3; |
| 232 | if (dry_run) { |
| 233 | read_into(name_buf, N_ALIGN(name_len), GotName); |
| 234 | return 0; |
| 235 | } |
| 236 | state = SkipIt; |
| 237 | if (name_len <= 0 || name_len > PATH_MAX) |
| 238 | return 0; |
| 239 | if (S_ISLNK(mode)) { |
| 240 | if (body_len > PATH_MAX) |
| 241 | return 0; |
| 242 | collect = collected = symlink_buf; |
| 243 | remains = N_ALIGN(name_len) + body_len; |
| 244 | next_state = GotSymlink; |
| 245 | state = Collect; |
| 246 | return 0; |
| 247 | } |
| 248 | if (S_ISREG(mode) || !body_len) |
| 249 | read_into(name_buf, N_ALIGN(name_len), GotName); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int __init do_skip(void) |
| 254 | { |
| 255 | if (this_header + count < next_header) { |
| 256 | eat(count); |
| 257 | return 1; |
| 258 | } else { |
| 259 | eat(next_header - this_header); |
| 260 | state = next_state; |
| 261 | return 0; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static int __init do_reset(void) |
| 266 | { |
| 267 | while(count && *victim == '\0') |
| 268 | eat(1); |
| 269 | if (count && (this_header & 3)) |
| 270 | error("broken padding"); |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | static int __init maybe_link(void) |
| 275 | { |
| 276 | if (nlink >= 2) { |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 277 | char *old = find_link(major, minor, ino, mode, collected); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (old) |
| 279 | return (sys_link(old, collected) < 0) ? -1 : 1; |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 284 | static void __init clean_path(char *path, mode_t mode) |
| 285 | { |
| 286 | struct stat st; |
| 287 | |
| 288 | if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) { |
| 289 | if (S_ISDIR(st.st_mode)) |
| 290 | sys_rmdir(path); |
| 291 | else |
| 292 | sys_unlink(path); |
| 293 | } |
| 294 | } |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | static __initdata int wfd; |
| 297 | |
| 298 | static int __init do_name(void) |
| 299 | { |
| 300 | state = SkipIt; |
| 301 | next_state = Reset; |
| 302 | if (strcmp(collected, "TRAILER!!!") == 0) { |
| 303 | free_hash(); |
| 304 | return 0; |
| 305 | } |
| 306 | if (dry_run) |
| 307 | return 0; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 308 | clean_path(collected, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (S_ISREG(mode)) { |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 310 | int ml = maybe_link(); |
| 311 | if (ml >= 0) { |
| 312 | int openflags = O_WRONLY|O_CREAT; |
| 313 | if (ml != 1) |
| 314 | openflags |= O_TRUNC; |
| 315 | wfd = sys_open(collected, openflags, mode); |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | if (wfd >= 0) { |
| 318 | sys_fchown(wfd, uid, gid); |
| 319 | sys_fchmod(wfd, mode); |
David Howells | cb6ff20 | 2009-01-08 12:04:48 +0000 | [diff] [blame] | 320 | sys_ftruncate(wfd, body_len); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 321 | vcollected = kstrdup(collected, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | state = CopyFile; |
| 323 | } |
| 324 | } |
| 325 | } else if (S_ISDIR(mode)) { |
| 326 | sys_mkdir(collected, mode); |
| 327 | sys_chown(collected, uid, gid); |
| 328 | sys_chmod(collected, mode); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 329 | dir_add(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } else if (S_ISBLK(mode) || S_ISCHR(mode) || |
| 331 | S_ISFIFO(mode) || S_ISSOCK(mode)) { |
| 332 | if (maybe_link() == 0) { |
| 333 | sys_mknod(collected, mode, rdev); |
| 334 | sys_chown(collected, uid, gid); |
| 335 | sys_chmod(collected, mode); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 336 | do_utime(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int __init do_copy(void) |
| 343 | { |
| 344 | if (count >= body_len) { |
| 345 | sys_write(wfd, victim, body_len); |
| 346 | sys_close(wfd); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 347 | do_utime(vcollected, mtime); |
| 348 | kfree(vcollected); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | eat(body_len); |
| 350 | state = SkipIt; |
| 351 | return 0; |
| 352 | } else { |
| 353 | sys_write(wfd, victim, count); |
| 354 | body_len -= count; |
| 355 | eat(count); |
| 356 | return 1; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static int __init do_symlink(void) |
| 361 | { |
| 362 | collected[N_ALIGN(name_len) + body_len] = '\0'; |
H. Peter Anvin | 2139a7f | 2006-06-26 00:28:02 -0700 | [diff] [blame] | 363 | clean_path(collected, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | sys_symlink(collected + N_ALIGN(name_len), collected); |
| 365 | sys_lchown(collected, uid, gid); |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 366 | do_utime(collected, mtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | state = SkipIt; |
| 368 | next_state = Reset; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static __initdata int (*actions[])(void) = { |
| 373 | [Start] = do_start, |
| 374 | [Collect] = do_collect, |
| 375 | [GotHeader] = do_header, |
| 376 | [SkipIt] = do_skip, |
| 377 | [GotName] = do_name, |
| 378 | [CopyFile] = do_copy, |
| 379 | [GotSymlink] = do_symlink, |
| 380 | [Reset] = do_reset, |
| 381 | }; |
| 382 | |
| 383 | static int __init write_buffer(char *buf, unsigned len) |
| 384 | { |
| 385 | count = len; |
| 386 | victim = buf; |
| 387 | |
| 388 | while (!actions[state]()) |
| 389 | ; |
| 390 | return len - count; |
| 391 | } |
| 392 | |
| 393 | static void __init flush_buffer(char *buf, unsigned len) |
| 394 | { |
| 395 | int written; |
| 396 | if (message) |
| 397 | return; |
| 398 | while ((written = write_buffer(buf, len)) < len && !message) { |
| 399 | char c = buf[written]; |
| 400 | if (c == '0') { |
| 401 | buf += written; |
| 402 | len -= written; |
| 403 | state = Start; |
| 404 | } else if (c == 0) { |
| 405 | buf += written; |
| 406 | len -= written; |
| 407 | state = Reset; |
| 408 | } else |
| 409 | error("junk in compressed archive"); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * gzip declarations |
| 415 | */ |
| 416 | |
| 417 | #define OF(args) args |
| 418 | |
| 419 | #ifndef memzero |
| 420 | #define memzero(s, n) memset ((s), 0, (n)) |
| 421 | #endif |
| 422 | |
| 423 | typedef unsigned char uch; |
| 424 | typedef unsigned short ush; |
| 425 | typedef unsigned long ulg; |
| 426 | |
| 427 | #define WSIZE 0x8000 /* window size--must be a power of two, and */ |
| 428 | /* at least 32K for zip's deflate method */ |
| 429 | |
| 430 | static uch *inbuf; |
| 431 | static uch *window; |
| 432 | |
| 433 | static unsigned insize; /* valid bytes in inbuf */ |
| 434 | static unsigned inptr; /* index of next byte to be processed in inbuf */ |
| 435 | static unsigned outcnt; /* bytes in output buffer */ |
| 436 | static long bytes_out; |
| 437 | |
| 438 | #define get_byte() (inptr < insize ? inbuf[inptr++] : -1) |
| 439 | |
| 440 | /* Diagnostic functions (stubbed out) */ |
| 441 | #define Assert(cond,msg) |
| 442 | #define Trace(x) |
| 443 | #define Tracev(x) |
| 444 | #define Tracevv(x) |
| 445 | #define Tracec(c,x) |
| 446 | #define Tracecv(c,x) |
| 447 | |
| 448 | #define STATIC static |
| 449 | #define INIT __init |
| 450 | |
| 451 | static void __init flush_window(void); |
| 452 | static void __init error(char *m); |
Thomas Petazzoni | 2d6ffcc | 2008-07-25 01:45:44 -0700 | [diff] [blame] | 453 | |
| 454 | #define NO_INFLATE_MALLOC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | #include "../lib/inflate.c" |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | /* =========================================================================== |
| 459 | * Write the output window window[0..outcnt-1] and update crc and bytes_out. |
| 460 | * (Used for the decompressed data only.) |
| 461 | */ |
| 462 | static void __init flush_window(void) |
| 463 | { |
| 464 | ulg c = crc; /* temporary variable */ |
| 465 | unsigned n; |
| 466 | uch *in, ch; |
| 467 | |
| 468 | flush_buffer(window, outcnt); |
| 469 | in = window; |
| 470 | for (n = 0; n < outcnt; n++) { |
| 471 | ch = *in++; |
| 472 | c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8); |
| 473 | } |
| 474 | crc = c; |
| 475 | bytes_out += (ulg)outcnt; |
| 476 | outcnt = 0; |
| 477 | } |
| 478 | |
| 479 | static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only) |
| 480 | { |
| 481 | int written; |
| 482 | dry_run = check_only; |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 483 | header_buf = kmalloc(110, GFP_KERNEL); |
| 484 | symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); |
| 485 | name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); |
| 486 | window = kmalloc(WSIZE, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | if (!window || !header_buf || !symlink_buf || !name_buf) |
| 488 | panic("can't allocate buffers"); |
| 489 | state = Start; |
| 490 | this_header = 0; |
| 491 | message = NULL; |
| 492 | while (!message && len) { |
| 493 | loff_t saved_offset = this_header; |
| 494 | if (*buf == '0' && !(this_header & 3)) { |
| 495 | state = Start; |
| 496 | written = write_buffer(buf, len); |
| 497 | buf += written; |
| 498 | len -= written; |
| 499 | continue; |
| 500 | } |
| 501 | if (!*buf) { |
| 502 | buf++; |
| 503 | len--; |
| 504 | this_header++; |
| 505 | continue; |
| 506 | } |
| 507 | this_header = 0; |
| 508 | insize = len; |
| 509 | inbuf = buf; |
| 510 | inptr = 0; |
| 511 | outcnt = 0; /* bytes in output buffer */ |
| 512 | bytes_out = 0; |
| 513 | crc = (ulg)0xffffffffL; /* shift register contents */ |
| 514 | makecrc(); |
| 515 | gunzip(); |
| 516 | if (state != Reset) |
| 517 | error("junk in gzipped archive"); |
| 518 | this_header = saved_offset + inptr; |
| 519 | buf += inptr; |
| 520 | len -= inptr; |
| 521 | } |
Nye Liu | 889d51a | 2008-10-15 22:01:40 -0700 | [diff] [blame] | 522 | dir_utime(); |
Thomas Petazzoni | 3265e66 | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 523 | kfree(window); |
| 524 | kfree(name_buf); |
| 525 | kfree(symlink_buf); |
| 526 | kfree(header_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | return message; |
| 528 | } |
| 529 | |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 530 | static int __initdata do_retain_initrd; |
| 531 | |
| 532 | static int __init retain_initrd_param(char *str) |
| 533 | { |
| 534 | if (*str) |
| 535 | return 0; |
| 536 | do_retain_initrd = 1; |
| 537 | return 1; |
| 538 | } |
| 539 | __setup("retain_initrd", retain_initrd_param); |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | extern char __initramfs_start[], __initramfs_end[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | #include <linux/initrd.h> |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 543 | #include <linux/kexec.h> |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 544 | |
| 545 | static void __init free_initrd(void) |
| 546 | { |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 547 | #ifdef CONFIG_KEXEC |
| 548 | unsigned long crashk_start = (unsigned long)__va(crashk_res.start); |
| 549 | unsigned long crashk_end = (unsigned long)__va(crashk_res.end); |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 550 | #endif |
| 551 | if (do_retain_initrd) |
| 552 | goto skip; |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 553 | |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 554 | #ifdef CONFIG_KEXEC |
Haren Myneni | 9c15e85 | 2006-02-10 01:51:05 -0800 | [diff] [blame] | 555 | /* |
| 556 | * If the initrd region is overlapped with crashkernel reserved region, |
| 557 | * free only memory that is not part of crashkernel region. |
| 558 | */ |
| 559 | if (initrd_start < crashk_end && initrd_end > crashk_start) { |
| 560 | /* |
| 561 | * Initialize initrd memory region since the kexec boot does |
| 562 | * not do. |
| 563 | */ |
| 564 | memset((void *)initrd_start, 0, initrd_end - initrd_start); |
| 565 | if (initrd_start < crashk_start) |
| 566 | free_initrd_mem(initrd_start, crashk_start); |
| 567 | if (initrd_end > crashk_end) |
| 568 | free_initrd_mem(crashk_end, initrd_end); |
| 569 | } else |
| 570 | #endif |
| 571 | free_initrd_mem(initrd_start, initrd_end); |
Michael Neuling | 0a7b35cb | 2007-02-10 01:44:33 -0800 | [diff] [blame] | 572 | skip: |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 573 | initrd_start = 0; |
| 574 | initrd_end = 0; |
| 575 | } |
| 576 | |
Linus Torvalds | 9a9e0d6 | 2008-03-15 11:53:32 -0700 | [diff] [blame] | 577 | static int __init populate_rootfs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | char *err = unpack_to_rootfs(__initramfs_start, |
| 580 | __initramfs_end - __initramfs_start, 0); |
| 581 | if (err) |
| 582 | panic(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | if (initrd_start) { |
Zdenek Pavlas | 340e48e | 2006-03-25 03:07:49 -0800 | [diff] [blame] | 584 | #ifdef CONFIG_BLK_DEV_RAM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | int fd; |
| 586 | printk(KERN_INFO "checking if image is initramfs..."); |
| 587 | err = unpack_to_rootfs((char *)initrd_start, |
| 588 | initrd_end - initrd_start, 1); |
| 589 | if (!err) { |
| 590 | printk(" it is\n"); |
| 591 | unpack_to_rootfs((char *)initrd_start, |
| 592 | initrd_end - initrd_start, 0); |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 593 | free_initrd(); |
Linus Torvalds | 8d610dd | 2006-12-11 12:12:04 -0800 | [diff] [blame] | 594 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | printk("it isn't (%s); looks like an initrd\n", err); |
Jason Gunthorpe | 33644c5 | 2006-03-26 01:37:38 -0800 | [diff] [blame] | 597 | fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | if (fd >= 0) { |
| 599 | sys_write(fd, (char *)initrd_start, |
| 600 | initrd_end - initrd_start); |
| 601 | sys_close(fd); |
Jan Beulich | 0f3d2bd | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 602 | free_initrd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
Zdenek Pavlas | 340e48e | 2006-03-25 03:07:49 -0800 | [diff] [blame] | 604 | #else |
| 605 | printk(KERN_INFO "Unpacking initramfs..."); |
| 606 | err = unpack_to_rootfs((char *)initrd_start, |
| 607 | initrd_end - initrd_start, 0); |
| 608 | if (err) |
| 609 | panic(err); |
| 610 | printk(" done\n"); |
| 611 | free_initrd(); |
| 612 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
Linus Torvalds | 8d610dd | 2006-12-11 12:12:04 -0800 | [diff] [blame] | 614 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
Linus Torvalds | 8d610dd | 2006-12-11 12:12:04 -0800 | [diff] [blame] | 616 | rootfs_initcall(populate_rootfs); |