dump9660.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <disk.h>
  13. #include <libsec.h>
  14. #include "iso9660.h"
  15. uint32_t now;
  16. int chatty;
  17. int doabort;
  18. int docolon;
  19. int mk9660;
  20. int64_t dataoffset;
  21. int blocksize;
  22. Conform *map;
  23. static void addprotofile(char *new, char *old, Dir *d, void *a);
  24. void usage(void);
  25. char *argv0;
  26. void
  27. usage(void)
  28. {
  29. if(mk9660)
  30. fprint(2, "usage: disk/mk9660 [-D:] [-9cjr] "
  31. "[-[bB] bootfile] [-o offset blocksize] "
  32. "[-p proto] [-s src] cdimage\n");
  33. else
  34. fprint(2, "usage: disk/dump9660 [-D:] [-9cjr] "
  35. "[-m maxsize] [-n now] "
  36. "[-p proto] [-s src] cdimage\n");
  37. exits("usage");
  38. }
  39. void
  40. main(int argc, char **argv)
  41. {
  42. int fix;
  43. uint32_t block, newnull, cblock;
  44. int64_t maxsize;
  45. uint64_t length, clength;
  46. char buf[256], *dumpname, *proto, *s, *src, *status;
  47. Cdimg *cd;
  48. Cdinfo info;
  49. XDir dir;
  50. Direc *iconform, idumproot, iroot, *jconform, jdumproot, jroot, *r;
  51. Dump *dump;
  52. fix = 0;
  53. status = nil;
  54. memset(&info, 0, sizeof info);
  55. proto = "/sys/lib/sysconfig/proto/allproto";
  56. src = "./";
  57. info.volumename = atom("9CD");
  58. info.volumeset = atom("9VolumeSet");
  59. info.publisher = atom("9Publisher");
  60. info.preparer = atom("dump9660");
  61. info.application = atom("dump9660");
  62. info.flags = CDdump;
  63. maxsize = 0;
  64. mk9660 = 0;
  65. fmtinstall('H', encodefmt);
  66. ARGBEGIN{
  67. case 'D':
  68. chatty++;
  69. break;
  70. case 'M':
  71. mk9660 = 1;
  72. argv0 = "disk/mk9660";
  73. info.flags &= ~CDdump;
  74. break;
  75. case '9':
  76. info.flags |= CDplan9;
  77. break;
  78. case ':':
  79. docolon = 1;
  80. break;
  81. case 'a':
  82. doabort = 1;
  83. break;
  84. case 'B':
  85. info.flags |= CDbootnoemu;
  86. /* fall through */
  87. case 'b':
  88. if(!mk9660)
  89. usage();
  90. info.flags |= CDbootable;
  91. info.bootimage = EARGF(usage());
  92. break;
  93. case 'c':
  94. info.flags |= CDconform;
  95. break;
  96. case 'f':
  97. fix = 1;
  98. break;
  99. case 'j':
  100. info.flags |= CDjoliet;
  101. break;
  102. case 'n':
  103. now = atoi(EARGF(usage()));
  104. break;
  105. case 'm':
  106. maxsize = strtoull(EARGF(usage()), 0, 0);
  107. break;
  108. case 'o':
  109. dataoffset = atoll(EARGF(usage()));
  110. blocksize = atoi(EARGF(usage()));
  111. if(blocksize%Blocksize)
  112. sysfatal("bad block size %d -- must be multiple of 2048", blocksize);
  113. blocksize /= Blocksize;
  114. break;
  115. case 'p':
  116. proto = EARGF(usage());
  117. break;
  118. case 'r':
  119. info.flags |= CDrockridge;
  120. break;
  121. case 's':
  122. src = EARGF(usage());
  123. break;
  124. case 'v':
  125. info.volumename = atom(EARGF(usage()));
  126. break;
  127. case 'x':
  128. info.flags |= CDpbs;
  129. info.loader = EARGF(usage());
  130. break;
  131. default:
  132. usage();
  133. }ARGEND
  134. if(info.flags & CDpbs && !(info.flags & CDbootnoemu))
  135. usage();
  136. if(mk9660 && (fix || now || maxsize))
  137. usage();
  138. if(argc != 1)
  139. usage();
  140. if(now == 0)
  141. now = (uint32_t)time(0);
  142. if(mk9660){
  143. if((cd = createcd(argv[0], info)) == nil)
  144. sysfatal("cannot create '%s': %r", argv[0]);
  145. }else{
  146. if((cd = opencd(argv[0], info)) == nil)
  147. sysfatal("cannot open '%s': %r", argv[0]);
  148. if(!(cd->flags & CDdump))
  149. sysfatal("not a dump cd");
  150. }
  151. /* create ISO9660/Plan 9 tree in memory */
  152. memset(&dir, 0, sizeof dir);
  153. dir.name = atom("");
  154. dir.uid = atom("sys");
  155. dir.gid = atom("sys");
  156. dir.uidno = 0;
  157. dir.gidno = 0;
  158. dir.mode = DMDIR | 0755;
  159. dir.mtime = now;
  160. dir.atime = now;
  161. dir.ctime = now;
  162. mkdirec(&iroot, &dir);
  163. iroot.srcfile = src;
  164. /*
  165. * Read new files into memory
  166. */
  167. if(rdproto(proto, src, addprotofile, nil, &iroot) < 0)
  168. sysfatal("rdproto: %r");
  169. if(mk9660){
  170. dump = emalloc(sizeof *dump);
  171. dumpname = nil;
  172. }else{
  173. /*
  174. * Read current dump tree and _conform.map.
  175. */
  176. idumproot = readdumpdirs(cd, &dir, isostring);
  177. readdumpconform(cd);
  178. if(cd->flags & CDjoliet)
  179. jdumproot = readdumpdirs(cd, &dir, jolietstring);
  180. if(fix){
  181. dumpname = nil;
  182. cd->nextblock = cd->nulldump+1;
  183. cd->nulldump = 0;
  184. Cwseek(cd, (int64_t)cd->nextblock * Blocksize);
  185. goto Dofix;
  186. }
  187. dumpname = adddumpdir(&idumproot, now, &dir);
  188. /* note that we assume all names are conforming and thus sorted */
  189. if(cd->flags & CDjoliet) {
  190. s = adddumpdir(&jdumproot, now, &dir);
  191. if(s != dumpname)
  192. sysfatal("dumpnames don't match %s %s", dumpname, s);
  193. }
  194. dump = dumpcd(cd, &idumproot);
  195. cd->nextblock = cd->nulldump+1;
  196. }
  197. /*
  198. * Write new files, starting where the dump tree was.
  199. * Must be done before creation of the Joliet tree so that
  200. * blocks and lengths are correct.
  201. */
  202. if(dataoffset > (int64_t)cd->nextblock * Blocksize)
  203. cd->nextblock = (dataoffset+Blocksize-1)/Blocksize;
  204. Cwseek(cd, (int64_t)cd->nextblock * Blocksize);
  205. writefiles(dump, cd, &iroot);
  206. if(cd->bootimage){
  207. findbootimage(cd, &iroot);
  208. if(cd->loader)
  209. findloader(cd, &iroot);
  210. Cupdatebootcat(cd);
  211. }
  212. /* create Joliet tree */
  213. if(cd->flags & CDjoliet)
  214. copydirec(&jroot, &iroot);
  215. if(info.flags & CDconform) {
  216. checknames(&iroot, isbadiso9660);
  217. convertnames(&iroot, struprcpy);
  218. } else
  219. convertnames(&iroot, (void *) strcpy);
  220. // isoabstract = findconform(&iroot, abstract);
  221. // isobiblio = findconform(&iroot, biblio);
  222. // isonotice = findconform(&iroot, notice);
  223. dsort(&iroot, isocmp);
  224. if(cd->flags & CDjoliet) {
  225. // jabstract = findconform(&jroot, abstract);
  226. // jbiblio = findconform(&jroot, biblio);
  227. // jnotice = findconform(&jroot, notice);
  228. checknames(&jroot, isbadjoliet);
  229. convertnames(&jroot, (void *) strcpy);
  230. dsort(&jroot, jolietcmp);
  231. }
  232. /*
  233. * Write directories.
  234. */
  235. writedirs(cd, &iroot, Cputisodir);
  236. if(cd->flags & CDjoliet)
  237. writedirs(cd, &jroot, Cputjolietdir);
  238. if(mk9660){
  239. cblock = 0;
  240. clength = 0;
  241. newnull = 0;
  242. }else{
  243. /*
  244. * Write incremental _conform.map block.
  245. */
  246. wrconform(cd, cd->nconform, &cblock, &clength);
  247. /* jump here if we're just fixing up the cd */
  248. Dofix:
  249. /*
  250. * Write null dump header block; everything after this will be
  251. * overwritten at the next dump. Because of this, it needs to be
  252. * reconstructable. We reconstruct the _conform.map and dump trees
  253. * from the header blocks in dump.c, and we reconstruct the path
  254. * tables by walking the cd.
  255. */
  256. newnull = Cputdumpblock(cd);
  257. }
  258. if(info.flags & CDpbs)
  259. Cfillpbs(cd);
  260. /*
  261. * Write _conform.map.
  262. */
  263. dir.mode = 0444;
  264. if(cd->flags & (CDconform|CDjoliet)) {
  265. if(!mk9660 && cd->nconform == 0){
  266. block = cblock;
  267. length = clength;
  268. }else
  269. wrconform(cd, 0, &block, &length);
  270. if(mk9660)
  271. {
  272. idumproot = iroot;
  273. jdumproot = jroot;
  274. }
  275. if(length) {
  276. /* The ISO9660 name will get turned into uppercase when written. */
  277. if((iconform = walkdirec(&idumproot, "_conform.map")) == nil)
  278. iconform = adddirec(&idumproot, "_conform.map", &dir);
  279. jconform = nil;
  280. if(cd->flags & CDjoliet) {
  281. if((jconform = walkdirec(&jdumproot, "_conform.map")) == nil)
  282. jconform = adddirec(&jdumproot, "_conform.map", &dir);
  283. }
  284. iconform->block = block;
  285. iconform->length = length;
  286. if(cd->flags & CDjoliet) {
  287. jconform->block = block;
  288. jconform->length = length;
  289. }
  290. }
  291. if(mk9660) {
  292. iroot = idumproot;
  293. jroot = jdumproot;
  294. }
  295. }
  296. if(mk9660){
  297. /*
  298. * Patch in root directories.
  299. */
  300. setroot(cd, cd->iso9660pvd, iroot.block, iroot.length);
  301. setvolsize(cd, cd->iso9660pvd, cd->nextblock);
  302. if(cd->flags & CDjoliet){
  303. setroot(cd, cd->jolietsvd, jroot.block, jroot.length);
  304. setvolsize(cd, cd->jolietsvd, cd->nextblock);
  305. }
  306. }else{
  307. /*
  308. * Write dump tree at end. We assume the name characters
  309. * are all conforming, so everything is already sorted properly.
  310. */
  311. convertnames(&idumproot, (info.flags & CDconform) ? (void *) struprcpy : (void *) strcpy);
  312. if(cd->nulldump) {
  313. r = walkdirec(&idumproot, dumpname);
  314. assert(r != nil);
  315. copybutname(r, &iroot);
  316. }
  317. if(cd->flags & CDjoliet) {
  318. convertnames(&jdumproot, (void *) strcpy);
  319. if(cd->nulldump) {
  320. r = walkdirec(&jdumproot, dumpname);
  321. assert(r != nil);
  322. copybutname(r, &jroot);
  323. }
  324. }
  325. writedumpdirs(cd, &idumproot, Cputisodir);
  326. if(cd->flags & CDjoliet)
  327. writedumpdirs(cd, &jdumproot, Cputjolietdir);
  328. /*
  329. * Patch in new root directory entry.
  330. */
  331. setroot(cd, cd->iso9660pvd, idumproot.block, idumproot.length);
  332. setvolsize(cd, cd->iso9660pvd, cd->nextblock);
  333. if(cd->flags & CDjoliet){
  334. setroot(cd, cd->jolietsvd, jdumproot.block, jdumproot.length);
  335. setvolsize(cd, cd->jolietsvd, cd->nextblock);
  336. }
  337. }
  338. writepathtables(cd);
  339. if(!mk9660){
  340. /*
  341. * If we've gotten too big, truncate back to what we started with,
  342. * fix up the cd, and exit with a non-zero status.
  343. */
  344. Cwflush(cd);
  345. if(cd->nulldump && maxsize && Cwoffset(cd) > maxsize){
  346. fprint(2, "too big; writing old tree back\n");
  347. status = "cd too big; aborted";
  348. rmdumpdir(&idumproot, dumpname);
  349. rmdumpdir(&jdumproot, dumpname);
  350. cd->nextblock = cd->nulldump+1;
  351. cd->nulldump = 0;
  352. Cwseek(cd, (int64_t)cd->nextblock * Blocksize);
  353. goto Dofix;
  354. }
  355. /*
  356. * Write old null header block; this commits all our changes.
  357. */
  358. if(cd->nulldump){
  359. Cwseek(cd, (int64_t)cd->nulldump * Blocksize);
  360. sprint(buf, "plan 9 dump cd\n");
  361. sprint(buf+strlen(buf), "%s %lu %lu %lu %llu %lu %lu",
  362. dumpname, now, newnull, cblock, clength,
  363. iroot.block, iroot.length);
  364. if(cd->flags & CDjoliet)
  365. sprint(buf+strlen(buf), " %lu %lu",
  366. jroot.block, jroot.length);
  367. strcat(buf, "\n");
  368. Cwrite(cd, buf, strlen(buf));
  369. Cpadblock(cd);
  370. Cwflush(cd);
  371. }
  372. }
  373. fdtruncate(cd->fd, (int64_t)cd->nextblock * Blocksize);
  374. exits(status);
  375. }
  376. static void
  377. addprotofile(char *new, char *old, Dir *d, void *a)
  378. {
  379. char *name, *p;
  380. Direc *direc;
  381. XDir xd;
  382. dirtoxdir(&xd, d);
  383. name = nil;
  384. if(docolon && strchr(new, ':')) {
  385. name = emalloc(strlen(new)+1);
  386. strcpy(name, new);
  387. while((p=strchr(name, ':')))
  388. *p=' ';
  389. new = name;
  390. }
  391. if((direc = adddirec((Direc*)a, new, &xd))) {
  392. direc->srcfile = atom(old);
  393. // BUG: abstract, biblio, notice
  394. }
  395. if(name)
  396. free(name);
  397. }