gen_init_cpio.c 13 KB

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