gen_init_cpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <ctype.h>
  11. #include <limits.h>
  12. /*
  13. * Original work by Jeff Garzik
  14. *
  15. * External file lists, symlink, pipe and fifo support by Thayne Harbaugh
  16. * Hard link support by Luciano Rocha
  17. */
  18. #define xstr(s) #s
  19. #define str(s) xstr(s)
  20. static unsigned int offset;
  21. static unsigned int ino = 721;
  22. static time_t default_mtime;
  23. struct file_handler {
  24. const char *type;
  25. int (*handler)(const char *line);
  26. };
  27. static void push_string(const char *name)
  28. {
  29. unsigned int name_len = strlen(name) + 1;
  30. fputs(name, stdout);
  31. putchar(0);
  32. offset += name_len;
  33. }
  34. static void push_pad (void)
  35. {
  36. while (offset & 3) {
  37. putchar(0);
  38. offset++;
  39. }
  40. }
  41. static void push_rest(const char *name)
  42. {
  43. unsigned int name_len = strlen(name) + 1;
  44. unsigned int tmp_ofs;
  45. fputs(name, stdout);
  46. putchar(0);
  47. offset += name_len;
  48. tmp_ofs = name_len + 110;
  49. while (tmp_ofs & 3) {
  50. putchar(0);
  51. offset++;
  52. tmp_ofs++;
  53. }
  54. }
  55. static void push_hdr(const char *s)
  56. {
  57. fputs(s, stdout);
  58. offset += 110;
  59. }
  60. static void cpio_trailer(void)
  61. {
  62. char s[256];
  63. const char name[] = "TRAILER!!!";
  64. sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX"
  65. "%08X%08X%08X%08X%08X%08X%08X",
  66. "070701", /* magic */
  67. 0, /* ino */
  68. 0, /* mode */
  69. (long) 0, /* uid */
  70. (long) 0, /* gid */
  71. 1, /* nlink */
  72. (long) 0, /* mtime */
  73. 0, /* filesize */
  74. 0, /* major */
  75. 0, /* minor */
  76. 0, /* rmajor */
  77. 0, /* rminor */
  78. (unsigned)strlen(name)+1, /* namesize */
  79. 0); /* chksum */
  80. push_hdr(s);
  81. push_rest(name);
  82. while (offset % 512) {
  83. putchar(0);
  84. offset++;
  85. }
  86. }
  87. static int cpio_mkslink(const char *name, const char *target,
  88. unsigned int mode, uid_t uid, gid_t gid)
  89. {
  90. char s[256];
  91. if (name[0] == '/')
  92. name++;
  93. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  94. "%08X%08X%08X%08X%08X%08X%08X",
  95. "070701", /* magic */
  96. ino++, /* ino */
  97. S_IFLNK | mode, /* mode */
  98. (long) uid, /* uid */
  99. (long) gid, /* gid */
  100. 1, /* nlink */
  101. (long) default_mtime, /* mtime */
  102. (unsigned)strlen(target)+1, /* filesize */
  103. 3, /* major */
  104. 1, /* minor */
  105. 0, /* rmajor */
  106. 0, /* rminor */
  107. (unsigned)strlen(name) + 1,/* namesize */
  108. 0); /* chksum */
  109. push_hdr(s);
  110. push_string(name);
  111. push_pad();
  112. push_string(target);
  113. push_pad();
  114. return 0;
  115. }
  116. static int cpio_mkslink_line(const char *line)
  117. {
  118. char name[PATH_MAX + 1];
  119. char target[PATH_MAX + 1];
  120. unsigned int mode;
  121. int uid;
  122. int gid;
  123. int rc = -1;
  124. if (5 != sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX) "s %o %d %d", name, target, &mode, &uid, &gid)) {
  125. fprintf(stderr, "Unrecognized dir format '%s'", line);
  126. goto fail;
  127. }
  128. rc = cpio_mkslink(name, target, mode, uid, gid);
  129. fail:
  130. return rc;
  131. }
  132. static int cpio_mkgeneric(const char *name, unsigned int mode,
  133. uid_t uid, gid_t gid)
  134. {
  135. char s[256];
  136. if (name[0] == '/')
  137. name++;
  138. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  139. "%08X%08X%08X%08X%08X%08X%08X",
  140. "070701", /* magic */
  141. ino++, /* ino */
  142. mode, /* mode */
  143. (long) uid, /* uid */
  144. (long) gid, /* gid */
  145. 2, /* nlink */
  146. (long) default_mtime, /* mtime */
  147. 0, /* filesize */
  148. 3, /* major */
  149. 1, /* minor */
  150. 0, /* rmajor */
  151. 0, /* rminor */
  152. (unsigned)strlen(name) + 1,/* namesize */
  153. 0); /* chksum */
  154. push_hdr(s);
  155. push_rest(name);
  156. return 0;
  157. }
  158. enum generic_types {
  159. GT_DIR,
  160. GT_PIPE,
  161. GT_SOCK
  162. };
  163. struct generic_type {
  164. const char *type;
  165. mode_t mode;
  166. };
  167. static struct generic_type generic_type_table[] = {
  168. [GT_DIR] = {
  169. .type = "dir",
  170. .mode = S_IFDIR
  171. },
  172. [GT_PIPE] = {
  173. .type = "pipe",
  174. .mode = S_IFIFO
  175. },
  176. [GT_SOCK] = {
  177. .type = "sock",
  178. .mode = S_IFSOCK
  179. }
  180. };
  181. static int cpio_mkgeneric_line(const char *line, enum generic_types gt)
  182. {
  183. char name[PATH_MAX + 1];
  184. unsigned int mode;
  185. int uid;
  186. int gid;
  187. int rc = -1;
  188. if (4 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d", name, &mode, &uid, &gid)) {
  189. fprintf(stderr, "Unrecognized %s format '%s'",
  190. line, generic_type_table[gt].type);
  191. goto fail;
  192. }
  193. mode |= generic_type_table[gt].mode;
  194. rc = cpio_mkgeneric(name, mode, uid, gid);
  195. fail:
  196. return rc;
  197. }
  198. static int cpio_mkdir_line(const char *line)
  199. {
  200. return cpio_mkgeneric_line(line, GT_DIR);
  201. }
  202. static int cpio_mkpipe_line(const char *line)
  203. {
  204. return cpio_mkgeneric_line(line, GT_PIPE);
  205. }
  206. static int cpio_mksock_line(const char *line)
  207. {
  208. return cpio_mkgeneric_line(line, GT_SOCK);
  209. }
  210. static int cpio_mknod(const char *name, unsigned int mode,
  211. uid_t uid, gid_t gid, char dev_type,
  212. unsigned int maj, unsigned int min)
  213. {
  214. char s[256];
  215. if (dev_type == 'b')
  216. mode |= S_IFBLK;
  217. else
  218. mode |= S_IFCHR;
  219. if (name[0] == '/')
  220. name++;
  221. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  222. "%08X%08X%08X%08X%08X%08X%08X",
  223. "070701", /* magic */
  224. ino++, /* ino */
  225. mode, /* mode */
  226. (long) uid, /* uid */
  227. (long) gid, /* gid */
  228. 1, /* nlink */
  229. (long) default_mtime, /* mtime */
  230. 0, /* filesize */
  231. 3, /* major */
  232. 1, /* minor */
  233. maj, /* rmajor */
  234. min, /* rminor */
  235. (unsigned)strlen(name) + 1,/* namesize */
  236. 0); /* chksum */
  237. push_hdr(s);
  238. push_rest(name);
  239. return 0;
  240. }
  241. static int cpio_mknod_line(const char *line)
  242. {
  243. char name[PATH_MAX + 1];
  244. unsigned int mode;
  245. int uid;
  246. int gid;
  247. char dev_type;
  248. unsigned int maj;
  249. unsigned int min;
  250. int rc = -1;
  251. if (7 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d %c %u %u",
  252. name, &mode, &uid, &gid, &dev_type, &maj, &min)) {
  253. fprintf(stderr, "Unrecognized nod format '%s'", line);
  254. goto fail;
  255. }
  256. rc = cpio_mknod(name, mode, uid, gid, dev_type, maj, min);
  257. fail:
  258. return rc;
  259. }
  260. static int cpio_mkfile(const char *name, const char *location,
  261. unsigned int mode, uid_t uid, gid_t gid,
  262. unsigned int nlinks)
  263. {
  264. char s[256];
  265. char *filebuf = NULL;
  266. struct stat buf;
  267. long size;
  268. int file = -1;
  269. int retval;
  270. int rc = -1;
  271. int namesize;
  272. int i;
  273. mode |= S_IFREG;
  274. file = open (location, O_RDONLY);
  275. if (file < 0) {
  276. fprintf (stderr, "File %s could not be opened for reading\n", location);
  277. goto error;
  278. }
  279. retval = fstat(file, &buf);
  280. if (retval) {
  281. fprintf(stderr, "File %s could not be stat()'ed\n", location);
  282. goto error;
  283. }
  284. filebuf = malloc(buf.st_size);
  285. if (!filebuf) {
  286. fprintf (stderr, "out of memory\n");
  287. goto error;
  288. }
  289. retval = read (file, filebuf, buf.st_size);
  290. if (retval < 0) {
  291. fprintf (stderr, "Can not read %s file\n", location);
  292. goto error;
  293. }
  294. size = 0;
  295. for (i = 1; i <= nlinks; i++) {
  296. /* data goes on last link */
  297. if (i == nlinks) size = buf.st_size;
  298. if (name[0] == '/')
  299. name++;
  300. namesize = strlen(name) + 1;
  301. sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
  302. "%08lX%08X%08X%08X%08X%08X%08X",
  303. "070701", /* magic */
  304. ino, /* ino */
  305. mode, /* mode */
  306. (long) uid, /* uid */
  307. (long) gid, /* gid */
  308. nlinks, /* nlink */
  309. (long) buf.st_mtime, /* mtime */
  310. size, /* filesize */
  311. 3, /* major */
  312. 1, /* minor */
  313. 0, /* rmajor */
  314. 0, /* rminor */
  315. namesize, /* namesize */
  316. 0); /* chksum */
  317. push_hdr(s);
  318. push_string(name);
  319. push_pad();
  320. if (size) {
  321. if (fwrite(filebuf, size, 1, stdout) != 1) {
  322. fprintf(stderr, "writing filebuf failed\n");
  323. goto error;
  324. }
  325. offset += size;
  326. push_pad();
  327. }
  328. name += namesize;
  329. }
  330. ino++;
  331. rc = 0;
  332. error:
  333. if (filebuf) free(filebuf);
  334. if (file >= 0) close(file);
  335. return rc;
  336. }
  337. static char *cpio_replace_env(char *new_location)
  338. {
  339. char expanded[PATH_MAX + 1];
  340. char env_var[PATH_MAX + 1];
  341. char *start;
  342. char *end;
  343. for (start = NULL; (start = strstr(new_location, "${")); ) {
  344. end = strchr(start, '}');
  345. if (start < end) {
  346. *env_var = *expanded = '\0';
  347. strncat(env_var, start + 2, end - start - 2);
  348. strncat(expanded, new_location, start - new_location);
  349. strncat(expanded, getenv(env_var), PATH_MAX);
  350. strncat(expanded, end + 1, PATH_MAX);
  351. strncpy(new_location, expanded, PATH_MAX);
  352. } else
  353. break;
  354. }
  355. return new_location;
  356. }
  357. static int cpio_mkfile_line(const char *line)
  358. {
  359. char name[PATH_MAX + 1];
  360. char *dname = NULL; /* malloc'ed buffer for hard links */
  361. char location[PATH_MAX + 1];
  362. unsigned int mode;
  363. int uid;
  364. int gid;
  365. int nlinks = 1;
  366. int end = 0, dname_len = 0;
  367. int rc = -1;
  368. if (5 > sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX)
  369. "s %o %d %d %n",
  370. name, location, &mode, &uid, &gid, &end)) {
  371. fprintf(stderr, "Unrecognized file format '%s'", line);
  372. goto fail;
  373. }
  374. if (end && isgraph(line[end])) {
  375. int len;
  376. int nend;
  377. dname = malloc(strlen(line));
  378. if (!dname) {
  379. fprintf (stderr, "out of memory (%d)\n", dname_len);
  380. goto fail;
  381. }
  382. dname_len = strlen(name) + 1;
  383. memcpy(dname, name, dname_len);
  384. do {
  385. nend = 0;
  386. if (sscanf(line + end, "%" str(PATH_MAX) "s %n",
  387. name, &nend) < 1)
  388. break;
  389. len = strlen(name) + 1;
  390. memcpy(dname + dname_len, name, len);
  391. dname_len += len;
  392. nlinks++;
  393. end += nend;
  394. } while (isgraph(line[end]));
  395. } else {
  396. dname = name;
  397. }
  398. rc = cpio_mkfile(dname, cpio_replace_env(location),
  399. mode, uid, gid, nlinks);
  400. fail:
  401. if (dname_len) free(dname);
  402. return rc;
  403. }
  404. static void usage(const char *prog)
  405. {
  406. fprintf(stderr, "Usage:\n"
  407. "\t%s [-t <timestamp>] <cpio_list>\n"
  408. "\n"
  409. "<cpio_list> is a file containing newline separated entries that\n"
  410. "describe the files to be included in the initramfs archive:\n"
  411. "\n"
  412. "# a comment\n"
  413. "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
  414. "dir <name> <mode> <uid> <gid>\n"
  415. "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
  416. "slink <name> <target> <mode> <uid> <gid>\n"
  417. "pipe <name> <mode> <uid> <gid>\n"
  418. "sock <name> <mode> <uid> <gid>\n"
  419. "\n"
  420. "<name> name of the file/dir/nod/etc in the archive\n"
  421. "<location> location of the file in the current filesystem\n"
  422. " expands shell variables quoted with ${}\n"
  423. "<target> link target\n"
  424. "<mode> mode/permissions of the file\n"
  425. "<uid> user id (0=root)\n"
  426. "<gid> group id (0=root)\n"
  427. "<dev_type> device type (b=block, c=character)\n"
  428. "<maj> major number of nod\n"
  429. "<min> minor number of nod\n"
  430. "<hard links> space separated list of other links to file\n"
  431. "\n"
  432. "example:\n"
  433. "# A simple initramfs\n"
  434. "dir /dev 0755 0 0\n"
  435. "nod /dev/console 0600 0 0 c 5 1\n"
  436. "dir /root 0700 0 0\n"
  437. "dir /sbin 0755 0 0\n"
  438. "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n"
  439. "\n"
  440. "<timestamp> is time in seconds since Epoch that will be used\n"
  441. "as mtime for symlinks, special files and directories. The default\n"
  442. "is to use the current time for these entries.\n",
  443. prog);
  444. }
  445. struct file_handler file_handler_table[] = {
  446. {
  447. .type = "file",
  448. .handler = cpio_mkfile_line,
  449. }, {
  450. .type = "nod",
  451. .handler = cpio_mknod_line,
  452. }, {
  453. .type = "dir",
  454. .handler = cpio_mkdir_line,
  455. }, {
  456. .type = "slink",
  457. .handler = cpio_mkslink_line,
  458. }, {
  459. .type = "pipe",
  460. .handler = cpio_mkpipe_line,
  461. }, {
  462. .type = "sock",
  463. .handler = cpio_mksock_line,
  464. }, {
  465. .type = NULL,
  466. .handler = NULL,
  467. }
  468. };
  469. #define LINE_SIZE (2 * PATH_MAX + 50)
  470. int main (int argc, char *argv[])
  471. {
  472. FILE *cpio_list;
  473. char line[LINE_SIZE];
  474. char *args, *type;
  475. int ec = 0;
  476. int line_nr = 0;
  477. const char *filename;
  478. default_mtime = time(NULL);
  479. while (1) {
  480. int opt = getopt(argc, argv, "t:h");
  481. char *invalid;
  482. if (opt == -1)
  483. break;
  484. switch (opt) {
  485. case 't':
  486. default_mtime = strtol(optarg, &invalid, 10);
  487. if (!*optarg || *invalid) {
  488. fprintf(stderr, "Invalid timestamp: %s\n",
  489. optarg);
  490. usage(argv[0]);
  491. exit(1);
  492. }
  493. break;
  494. case 'h':
  495. case '?':
  496. usage(argv[0]);
  497. exit(opt == 'h' ? 0 : 1);
  498. }
  499. }
  500. if (argc - optind != 1) {
  501. usage(argv[0]);
  502. exit(1);
  503. }
  504. filename = argv[optind];
  505. if (!strcmp(filename, "-"))
  506. cpio_list = stdin;
  507. else if (!(cpio_list = fopen(filename, "r"))) {
  508. fprintf(stderr, "ERROR: unable to open '%s': %s\n\n",
  509. filename, strerror(errno));
  510. usage(argv[0]);
  511. exit(1);
  512. }
  513. while (fgets(line, LINE_SIZE, cpio_list)) {
  514. int type_idx;
  515. size_t slen = strlen(line);
  516. line_nr++;
  517. if ('#' == *line) {
  518. /* comment - skip to next line */
  519. continue;
  520. }
  521. if (! (type = strtok(line, " \t"))) {
  522. fprintf(stderr,
  523. "ERROR: incorrect format, could not locate file type line %d: '%s'\n",
  524. line_nr, line);
  525. ec = -1;
  526. break;
  527. }
  528. if ('\n' == *type) {
  529. /* a blank line */
  530. continue;
  531. }
  532. if (slen == strlen(type)) {
  533. /* must be an empty line */
  534. continue;
  535. }
  536. if (! (args = strtok(NULL, "\n"))) {
  537. fprintf(stderr,
  538. "ERROR: incorrect format, newline required line %d: '%s'\n",
  539. line_nr, line);
  540. ec = -1;
  541. }
  542. for (type_idx = 0; file_handler_table[type_idx].type; type_idx++) {
  543. int rc;
  544. if (! strcmp(line, file_handler_table[type_idx].type)) {
  545. if ((rc = file_handler_table[type_idx].handler(args))) {
  546. ec = rc;
  547. fprintf(stderr, " line %d\n", line_nr);
  548. }
  549. break;
  550. }
  551. }
  552. if (NULL == file_handler_table[type_idx].type) {
  553. fprintf(stderr, "unknown file type line %d: '%s'\n",
  554. line_nr, line);
  555. }
  556. }
  557. if (ec == 0)
  558. cpio_trailer();
  559. exit(ec);
  560. }