gen_init_cpio.c 11 KB

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