mkbootfs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <dirent.h>
  9. #include <stdarg.h>
  10. #include <fcntl.h>
  11. #include <private/android_filesystem_config.h>
  12. /* NOTES
  13. **
  14. ** - see buffer-format.txt from the linux kernel docs for
  15. ** an explanation of this file format
  16. ** - dotfiles are ignored
  17. ** - directories named 'root' are ignored
  18. ** - device notes, pipes, etc are not supported (error)
  19. */
  20. void die(const char *why, ...)
  21. {
  22. va_list ap;
  23. va_start(ap, why);
  24. fprintf(stderr,"error: ");
  25. vfprintf(stderr, why, ap);
  26. fprintf(stderr,"\n");
  27. va_end(ap);
  28. exit(1);
  29. }
  30. struct fs_config_entry {
  31. char* name;
  32. int uid, gid, mode;
  33. };
  34. static struct fs_config_entry* canned_config = NULL;
  35. static char *target_out_path = NULL;
  36. /* Each line in the canned file should be a path plus three ints (uid,
  37. * gid, mode). */
  38. #ifdef PATH_MAX
  39. #define CANNED_LINE_LENGTH (PATH_MAX+100)
  40. #else
  41. #define CANNED_LINE_LENGTH (1024)
  42. #endif
  43. #define TRAILER "TRAILER!!!"
  44. static int verbose = 0;
  45. static int total_size = 0;
  46. static void fix_stat(const char *path, struct stat *s)
  47. {
  48. uint64_t capabilities;
  49. if (canned_config) {
  50. // Use the list of file uid/gid/modes loaded from the file
  51. // given with -f.
  52. struct fs_config_entry* empty_path_config = NULL;
  53. struct fs_config_entry* p;
  54. for (p = canned_config; p->name; ++p) {
  55. if (!p->name[0]) {
  56. empty_path_config = p;
  57. }
  58. if (strcmp(p->name, path) == 0) {
  59. s->st_uid = p->uid;
  60. s->st_gid = p->gid;
  61. s->st_mode = p->mode | (s->st_mode & ~07777);
  62. return;
  63. }
  64. }
  65. s->st_uid = empty_path_config->uid;
  66. s->st_gid = empty_path_config->gid;
  67. s->st_mode = empty_path_config->mode | (s->st_mode & ~07777);
  68. } else {
  69. // Use the compiled-in fs_config() function.
  70. unsigned st_mode = s->st_mode;
  71. int is_dir = S_ISDIR(s->st_mode) || strcmp(path, TRAILER) == 0;
  72. fs_config(path, is_dir, target_out_path, &s->st_uid, &s->st_gid, &st_mode, &capabilities);
  73. s->st_mode = (typeof(s->st_mode)) st_mode;
  74. }
  75. }
  76. static void _eject(struct stat *s, char *out, int olen, char *data, unsigned datasize)
  77. {
  78. // Nothing is special about this value, just picked something in the
  79. // approximate range that was being used already, and avoiding small
  80. // values which may be special.
  81. static unsigned next_inode = 300000;
  82. while(total_size & 3) {
  83. total_size++;
  84. putchar(0);
  85. }
  86. fix_stat(out, s);
  87. // fprintf(stderr, "_eject %s: mode=0%o\n", out, s->st_mode);
  88. printf("%06x%08x%08x%08x%08x%08x%08x"
  89. "%08x%08x%08x%08x%08x%08x%08x%s%c",
  90. 0x070701,
  91. next_inode++, // s.st_ino,
  92. s->st_mode,
  93. 0, // s.st_uid,
  94. 0, // s.st_gid,
  95. 1, // s.st_nlink,
  96. 0, // s.st_mtime,
  97. datasize,
  98. 0, // volmajor
  99. 0, // volminor
  100. 0, // devmajor
  101. 0, // devminor,
  102. olen + 1,
  103. 0,
  104. out,
  105. 0
  106. );
  107. total_size += 6 + 8*13 + olen + 1;
  108. if(strlen(out) != (unsigned int)olen) die("ACK!");
  109. while(total_size & 3) {
  110. total_size++;
  111. putchar(0);
  112. }
  113. if(datasize) {
  114. fwrite(data, datasize, 1, stdout);
  115. total_size += datasize;
  116. }
  117. }
  118. static void _eject_trailer()
  119. {
  120. struct stat s;
  121. memset(&s, 0, sizeof(s));
  122. _eject(&s, TRAILER, 10, 0, 0);
  123. while(total_size & 0xff) {
  124. total_size++;
  125. putchar(0);
  126. }
  127. }
  128. static void _archive(char *in, char *out, int ilen, int olen);
  129. static int compare(const void* a, const void* b) {
  130. return strcmp(*(const char**)a, *(const char**)b);
  131. }
  132. static void _archive_dir(char *in, char *out, int ilen, int olen)
  133. {
  134. int i, t;
  135. DIR *d;
  136. struct dirent *de;
  137. if(verbose) {
  138. fprintf(stderr,"_archive_dir('%s','%s',%d,%d)\n",
  139. in, out, ilen, olen);
  140. }
  141. d = opendir(in);
  142. if(d == 0) die("cannot open directory '%s'", in);
  143. int size = 32;
  144. int entries = 0;
  145. char** names = malloc(size * sizeof(char*));
  146. if (names == NULL) {
  147. fprintf(stderr, "failed to allocate dir names array (size %d)\n", size);
  148. exit(1);
  149. }
  150. while((de = readdir(d)) != 0){
  151. /* xxx: feature? maybe some dotfiles are okay */
  152. if(de->d_name[0] == '.') continue;
  153. /* xxx: hack. use a real exclude list */
  154. if(!strcmp(de->d_name, "root")) continue;
  155. if (entries >= size) {
  156. size *= 2;
  157. names = realloc(names, size * sizeof(char*));
  158. if (names == NULL) {
  159. fprintf(stderr, "failed to reallocate dir names array (size %d)\n",
  160. size);
  161. exit(1);
  162. }
  163. }
  164. names[entries] = strdup(de->d_name);
  165. if (names[entries] == NULL) {
  166. fprintf(stderr, "failed to strdup name \"%s\"\n",
  167. de->d_name);
  168. exit(1);
  169. }
  170. ++entries;
  171. }
  172. qsort(names, entries, sizeof(char*), compare);
  173. for (i = 0; i < entries; ++i) {
  174. t = strlen(names[i]);
  175. in[ilen] = '/';
  176. memcpy(in + ilen + 1, names[i], t + 1);
  177. if(olen > 0) {
  178. out[olen] = '/';
  179. memcpy(out + olen + 1, names[i], t + 1);
  180. _archive(in, out, ilen + t + 1, olen + t + 1);
  181. } else {
  182. memcpy(out, names[i], t + 1);
  183. _archive(in, out, ilen + t + 1, t);
  184. }
  185. in[ilen] = 0;
  186. out[olen] = 0;
  187. free(names[i]);
  188. }
  189. free(names);
  190. closedir(d);
  191. }
  192. static void _archive(char *in, char *out, int ilen, int olen)
  193. {
  194. struct stat s;
  195. if(verbose) {
  196. fprintf(stderr,"_archive('%s','%s',%d,%d)\n",
  197. in, out, ilen, olen);
  198. }
  199. if(lstat(in, &s)) die("could not stat '%s'\n", in);
  200. if(S_ISREG(s.st_mode)){
  201. char *tmp;
  202. int fd;
  203. fd = open(in, O_RDONLY);
  204. if(fd < 0) die("cannot open '%s' for read", in);
  205. tmp = (char*) malloc(s.st_size);
  206. if(tmp == 0) die("cannot allocate %d bytes", s.st_size);
  207. if(read(fd, tmp, s.st_size) != s.st_size) {
  208. die("cannot read %d bytes", s.st_size);
  209. }
  210. _eject(&s, out, olen, tmp, s.st_size);
  211. free(tmp);
  212. close(fd);
  213. } else if(S_ISDIR(s.st_mode)) {
  214. _eject(&s, out, olen, 0, 0);
  215. _archive_dir(in, out, ilen, olen);
  216. } else if(S_ISLNK(s.st_mode)) {
  217. char buf[1024];
  218. int size;
  219. size = readlink(in, buf, 1024);
  220. if(size < 0) die("cannot read symlink '%s'", in);
  221. _eject(&s, out, olen, buf, size);
  222. } else {
  223. die("Unknown '%s' (mode %d)?\n", in, s.st_mode);
  224. }
  225. }
  226. void archive(const char *start, const char *prefix)
  227. {
  228. char in[8192];
  229. char out[8192];
  230. strcpy(in, start);
  231. strcpy(out, prefix);
  232. _archive_dir(in, out, strlen(in), strlen(out));
  233. }
  234. static void read_canned_config(char* filename)
  235. {
  236. int allocated = 8;
  237. int used = 0;
  238. canned_config =
  239. (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
  240. char line[CANNED_LINE_LENGTH];
  241. FILE* f = fopen(filename, "r");
  242. if (f == NULL) die("failed to open canned file");
  243. while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) {
  244. if (!line[0]) break;
  245. if (used >= allocated) {
  246. allocated *= 2;
  247. canned_config = (struct fs_config_entry*)realloc(
  248. canned_config, allocated * sizeof(struct fs_config_entry));
  249. if (canned_config == NULL) die("failed to reallocate memory");
  250. }
  251. struct fs_config_entry* cc = canned_config + used;
  252. if (isspace(line[0])) {
  253. cc->name = strdup("");
  254. cc->uid = atoi(strtok(line, " \n"));
  255. } else {
  256. cc->name = strdup(strtok(line, " \n"));
  257. cc->uid = atoi(strtok(NULL, " \n"));
  258. }
  259. cc->gid = atoi(strtok(NULL, " \n"));
  260. cc->mode = strtol(strtok(NULL, " \n"), NULL, 8);
  261. ++used;
  262. }
  263. if (used >= allocated) {
  264. ++allocated;
  265. canned_config = (struct fs_config_entry*)realloc(
  266. canned_config, allocated * sizeof(struct fs_config_entry));
  267. if (canned_config == NULL) die("failed to reallocate memory");
  268. }
  269. canned_config[used].name = NULL;
  270. fclose(f);
  271. }
  272. int main(int argc, char *argv[])
  273. {
  274. argc--;
  275. argv++;
  276. if (argc > 1 && strcmp(argv[0], "-d") == 0) {
  277. target_out_path = argv[1];
  278. argc -= 2;
  279. argv += 2;
  280. }
  281. if (argc > 1 && strcmp(argv[0], "-f") == 0) {
  282. read_canned_config(argv[1]);
  283. argc -= 2;
  284. argv += 2;
  285. }
  286. if(argc == 0) die("no directories to process?!");
  287. while(argc-- > 0){
  288. char *x = strchr(*argv, '=');
  289. if(x != 0) {
  290. *x++ = 0;
  291. } else {
  292. x = "";
  293. }
  294. archive(*argv, x);
  295. argv++;
  296. }
  297. _eject_trailer();
  298. return 0;
  299. }