mkrootfs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* mkrootfs: wrap a gzip command pack in a bootable rootfs image.
  2. *
  3. * Usage: mkrootfs <out.img> <pack.gz> [size]
  4. *
  5. * The image starts with a struct rootfs_hdr (rootfs.h) in its first 512-byte
  6. * sector and the pack at byte 512. A non-zero size pads the image to that
  7. * many bytes (the USB rootfs partition); size 0 keeps it minimal (the floppy
  8. * image appends the header + pack directly after the kernel).
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include "rootfs.h"
  15. static void put32le(unsigned char *p, uint32_t v)
  16. {
  17. p[0] = (unsigned char)(v & 0xFF);
  18. p[1] = (unsigned char)((v >> 8) & 0xFF);
  19. p[2] = (unsigned char)((v >> 16) & 0xFF);
  20. p[3] = (unsigned char)((v >> 24) & 0xFF);
  21. }
  22. int main(int argc, char **argv)
  23. {
  24. unsigned char hdr[ROOTFS_HDR_SIZE];
  25. const char *out_path, *pack_path;
  26. unsigned long size = 0, total;
  27. unsigned char *pack;
  28. uint32_t csum = 0;
  29. FILE *f;
  30. long n;
  31. unsigned long i;
  32. if (argc != 3 && argc != 4) {
  33. fprintf(stderr, "usage: %s <out.img> <pack.gz> [size]\n",
  34. argv[0]);
  35. return 1;
  36. }
  37. out_path = argv[1];
  38. pack_path = argv[2];
  39. if (argc == 4) {
  40. char *end;
  41. size = strtoul(argv[3], &end, 0);
  42. if (*end || size == 0) {
  43. fprintf(stderr, "mkrootfs: bad size '%s'\n", argv[3]);
  44. return 1;
  45. }
  46. }
  47. f = fopen(pack_path, "rb");
  48. if (!f) {
  49. fprintf(stderr, "mkrootfs: %s: cannot open\n", pack_path);
  50. return 1;
  51. }
  52. fseek(f, 0, SEEK_END);
  53. n = ftell(f);
  54. fseek(f, 0, SEEK_SET);
  55. if (n <= 0) {
  56. fprintf(stderr, "mkrootfs: %s: empty\n", pack_path);
  57. fclose(f);
  58. return 1;
  59. }
  60. pack = malloc((size_t)n);
  61. if (!pack || fread(pack, 1, (size_t)n, f) != (size_t)n) {
  62. fprintf(stderr, "mkrootfs: %s: read failed\n", pack_path);
  63. fclose(f);
  64. free(pack);
  65. return 1;
  66. }
  67. fclose(f);
  68. for (i = 0; i < (unsigned long)n; i++)
  69. csum += pack[i];
  70. total = ROOTFS_HDR_SIZE + (unsigned long)n;
  71. if (size && size < total) {
  72. fprintf(stderr, "mkrootfs: size %lu is smaller than the image "
  73. "(%lu bytes)\n", size, total);
  74. free(pack);
  75. return 1;
  76. }
  77. memset(hdr, 0, sizeof(hdr));
  78. memcpy(hdr, ROOTFS_MAGIC, 7);
  79. put32le(hdr + 8, ROOTFS_VERSION);
  80. put32le(hdr + 12, (uint32_t)n);
  81. put32le(hdr + 16, csum);
  82. put32le(hdr + 20, ROOTFS_FLAG_GZIP);
  83. f = fopen(out_path, "wb");
  84. if (!f) {
  85. fprintf(stderr, "mkrootfs: %s: cannot create\n", out_path);
  86. free(pack);
  87. return 1;
  88. }
  89. if (fwrite(hdr, 1, sizeof(hdr), f) != sizeof(hdr) ||
  90. fwrite(pack, 1, (size_t)n, f) != (size_t)n) {
  91. fprintf(stderr, "mkrootfs: %s: write failed\n", out_path);
  92. fclose(f);
  93. free(pack);
  94. return 1;
  95. }
  96. free(pack);
  97. if (size) {
  98. static const unsigned char zero[4096];
  99. for (i = total; i < size; ) {
  100. unsigned long chunk = size - i;
  101. if (chunk > sizeof(zero))
  102. chunk = sizeof(zero);
  103. if (fwrite(zero, 1, chunk, f) != chunk) {
  104. fprintf(stderr, "mkrootfs: %s: pad failed\n",
  105. out_path);
  106. fclose(f);
  107. return 1;
  108. }
  109. i += chunk;
  110. }
  111. }
  112. if (fclose(f) != 0) {
  113. fprintf(stderr, "mkrootfs: %s: close failed\n", out_path);
  114. return 1;
  115. }
  116. fprintf(stderr, "mkrootfs: %s: pack %ld bytes, total %lu bytes\n",
  117. out_path, n, size ? size : total);
  118. return 0;
  119. }