flfmt9660.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Initialize a fossil file system from an ISO9660 image already in the
  3. * file system. This is a fairly bizarre thing to do, but it lets us generate
  4. * installation CDs that double as valid Plan 9 disk partitions.
  5. * People having trouble booting the CD can just copy it into a disk
  6. * partition and you've got a working Plan 9 system.
  7. *
  8. * I've tried hard to keep all the associated cruft in this file.
  9. * If you deleted this file and cut out the three calls into it from flfmt.c,
  10. * no traces would remain.
  11. */
  12. #include "stdinc.h"
  13. #include "dat.h"
  14. #include "fns.h"
  15. #include "flfmt9660.h"
  16. #include <bio.h>
  17. #include <ctype.h>
  18. static Biobuf *b;
  19. enum{
  20. Tag = 0x96609660,
  21. Blocksize = 2048,
  22. };
  23. #pragma varargck type "s" uchar*
  24. #pragma varargck type "L" uchar*
  25. #pragma varargck type "B" uchar*
  26. #pragma varargck type "N" uchar*
  27. #pragma varargck type "T" uchar*
  28. #pragma varargck type "D" uchar*
  29. typedef struct Voldesc Voldesc;
  30. struct Voldesc {
  31. uchar magic[8]; /* 0x01, "CD001", 0x01, 0x00 */
  32. uchar systemid[32]; /* system identifier */
  33. uchar volumeid[32]; /* volume identifier */
  34. uchar unused[8]; /* character set in secondary desc */
  35. uchar volsize[8]; /* volume size */
  36. uchar charset[32];
  37. uchar volsetsize[4]; /* volume set size = 1 */
  38. uchar volseqnum[4]; /* volume sequence number = 1 */
  39. uchar blocksize[4]; /* logical block size */
  40. uchar pathsize[8]; /* path table size */
  41. uchar lpathloc[4]; /* Lpath */
  42. uchar olpathloc[4]; /* optional Lpath */
  43. uchar mpathloc[4]; /* Mpath */
  44. uchar ompathloc[4]; /* optional Mpath */
  45. uchar rootdir[34]; /* root directory */
  46. uchar volsetid[128]; /* volume set identifier */
  47. uchar publisher[128];
  48. uchar prepid[128]; /* data preparer identifier */
  49. uchar applid[128]; /* application identifier */
  50. uchar notice[37]; /* copyright notice file */
  51. uchar abstract[37]; /* abstract file */
  52. uchar biblio[37]; /* bibliographic file */
  53. uchar cdate[17]; /* creation date */
  54. uchar mdate[17]; /* modification date */
  55. uchar xdate[17]; /* expiration date */
  56. uchar edate[17]; /* effective date */
  57. uchar fsvers; /* file system version = 1 */
  58. };
  59. static void
  60. dumpbootvol(void *a)
  61. {
  62. Voldesc *v;
  63. v = a;
  64. print("magic %.2ux %.5s %.2ux %2ux\n",
  65. v->magic[0], v->magic+1, v->magic[6], v->magic[7]);
  66. if(v->magic[0] == 0xFF)
  67. return;
  68. print("system %.32T\n", v->systemid);
  69. print("volume %.32T\n", v->volumeid);
  70. print("volume size %.4N\n", v->volsize);
  71. print("charset %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux\n",
  72. v->charset[0], v->charset[1], v->charset[2], v->charset[3],
  73. v->charset[4], v->charset[5], v->charset[6], v->charset[7]);
  74. print("volume set size %.2N\n", v->volsetsize);
  75. print("volume sequence number %.2N\n", v->volseqnum);
  76. print("logical block size %.2N\n", v->blocksize);
  77. print("path size %.4L\n", v->pathsize);
  78. print("lpath loc %.4L\n", v->lpathloc);
  79. print("opt lpath loc %.4L\n", v->olpathloc);
  80. print("mpath loc %.4B\n", v->mpathloc);
  81. print("opt mpath loc %.4B\n", v->ompathloc);
  82. print("rootdir %D\n", v->rootdir);
  83. print("volume set identifier %.128T\n", v->volsetid);
  84. print("publisher %.128T\n", v->publisher);
  85. print("preparer %.128T\n", v->prepid);
  86. print("application %.128T\n", v->applid);
  87. print("notice %.37T\n", v->notice);
  88. print("abstract %.37T\n", v->abstract);
  89. print("biblio %.37T\n", v->biblio);
  90. print("creation date %.17s\n", v->cdate);
  91. print("modification date %.17s\n", v->mdate);
  92. print("expiration date %.17s\n", v->xdate);
  93. print("effective date %.17s\n", v->edate);
  94. print("fs version %d\n", v->fsvers);
  95. }
  96. typedef struct Cdir Cdir;
  97. struct Cdir {
  98. uchar len;
  99. uchar xlen;
  100. uchar dloc[8];
  101. uchar dlen[8];
  102. uchar date[7];
  103. uchar flags;
  104. uchar unitsize;
  105. uchar gapsize;
  106. uchar volseqnum[4];
  107. uchar namelen;
  108. uchar name[1]; /* chumminess */
  109. };
  110. #pragma varargck type "D" Cdir*
  111. static int
  112. Dfmt(Fmt *fmt)
  113. {
  114. char buf[128];
  115. Cdir *c;
  116. c = va_arg(fmt->args, Cdir*);
  117. if(c->namelen == 1 && c->name[0] == '\0' || c->name[0] == '\001') {
  118. snprint(buf, sizeof buf, ".%s dloc %.4N dlen %.4N",
  119. c->name[0] ? "." : "", c->dloc, c->dlen);
  120. } else {
  121. snprint(buf, sizeof buf, "%.*T dloc %.4N dlen %.4N", c->namelen, c->name,
  122. c->dloc, c->dlen);
  123. }
  124. fmtstrcpy(fmt, buf);
  125. return 0;
  126. }
  127. char longc, shortc;
  128. static void
  129. bigend(void)
  130. {
  131. longc = 'B';
  132. }
  133. static void
  134. littleend(void)
  135. {
  136. longc = 'L';
  137. }
  138. static ulong
  139. big(void *a, int n)
  140. {
  141. uchar *p;
  142. ulong v;
  143. int i;
  144. p = a;
  145. v = 0;
  146. for(i=0; i<n; i++)
  147. v = (v<<8) | *p++;
  148. return v;
  149. }
  150. static ulong
  151. little(void *a, int n)
  152. {
  153. uchar *p;
  154. ulong v;
  155. int i;
  156. p = a;
  157. v = 0;
  158. for(i=0; i<n; i++)
  159. v |= (*p++<<(i*8));
  160. return v;
  161. }
  162. /* numbers in big or little endian. */
  163. static int
  164. BLfmt(Fmt *fmt)
  165. {
  166. ulong v;
  167. uchar *p;
  168. char buf[20];
  169. p = va_arg(fmt->args, uchar*);
  170. if(!(fmt->flags&FmtPrec)) {
  171. fmtstrcpy(fmt, "*BL*");
  172. return 0;
  173. }
  174. if(fmt->r == 'B')
  175. v = big(p, fmt->prec);
  176. else
  177. v = little(p, fmt->prec);
  178. sprint(buf, "0x%.*lux", fmt->prec*2, v);
  179. fmt->flags &= ~FmtPrec;
  180. fmtstrcpy(fmt, buf);
  181. return 0;
  182. }
  183. /* numbers in both little and big endian */
  184. static int
  185. Nfmt(Fmt *fmt)
  186. {
  187. char buf[100];
  188. uchar *p;
  189. p = va_arg(fmt->args, uchar*);
  190. sprint(buf, "%.*L %.*B", fmt->prec, p, fmt->prec, p+fmt->prec);
  191. fmt->flags &= ~FmtPrec;
  192. fmtstrcpy(fmt, buf);
  193. return 0;
  194. }
  195. static int
  196. asciiTfmt(Fmt *fmt)
  197. {
  198. char *p, buf[256];
  199. int i;
  200. p = va_arg(fmt->args, char*);
  201. for(i=0; i<fmt->prec; i++)
  202. buf[i] = *p++;
  203. buf[i] = '\0';
  204. for(p=buf+strlen(buf); p>buf && p[-1]==' '; p--)
  205. ;
  206. p[0] = '\0';
  207. fmt->flags &= ~FmtPrec;
  208. fmtstrcpy(fmt, buf);
  209. return 0;
  210. }
  211. static void
  212. ascii(void)
  213. {
  214. fmtinstall('T', asciiTfmt);
  215. }
  216. static int
  217. runeTfmt(Fmt *fmt)
  218. {
  219. Rune buf[256], *r;
  220. int i;
  221. uchar *p;
  222. p = va_arg(fmt->args, uchar*);
  223. for(i=0; i*2+2<=fmt->prec; i++, p+=2)
  224. buf[i] = (p[0]<<8)|p[1];
  225. buf[i] = L'\0';
  226. for(r=buf+i; r>buf && r[-1]==L' '; r--)
  227. ;
  228. r[0] = L'\0';
  229. fmt->flags &= ~FmtPrec;
  230. return fmtprint(fmt, "%S", buf);
  231. }
  232. static void
  233. getsect(uchar *buf, int n)
  234. {
  235. if(Bseek(b, n*2048, 0) != n*2048 || Bread(b, buf, 2048) != 2048)
  236. {
  237. abort();
  238. sysfatal("reading block at %,d: %r\n", n*2048);
  239. }
  240. }
  241. static Header *h;
  242. static int fd;
  243. static char *file9660;
  244. static int off9660;
  245. static ulong startoff;
  246. static ulong endoff;
  247. static ulong fsoff;
  248. static uchar root[2048];
  249. static Voldesc *v;
  250. static ulong iso9660start(Cdir*);
  251. static void iso9660copydir(Fs*, File*, Cdir*);
  252. static void iso9660copyfile(Fs*, File*, Cdir*);
  253. void
  254. iso9660init(int xfd, Header *xh, char *xfile9660, int xoff9660)
  255. {
  256. uchar sect[2048], sect2[2048];
  257. fmtinstall('L', BLfmt);
  258. fmtinstall('B', BLfmt);
  259. fmtinstall('N', Nfmt);
  260. fmtinstall('D', Dfmt);
  261. fd = xfd;
  262. h = xh;
  263. file9660 = xfile9660;
  264. off9660 = xoff9660;
  265. if((b = Bopen(file9660, OREAD)) == nil)
  266. vtFatal("Bopen %s: %r", file9660);
  267. getsect(root, 16);
  268. ascii();
  269. v = (Voldesc*)root;
  270. if(memcmp(v->magic, "\x01CD001\x01\x00", 8) != 0)
  271. vtFatal("%s not a cd image", file9660);
  272. startoff = iso9660start((Cdir*)v->rootdir)*Blocksize;
  273. endoff = little(v->volsize, 4); /* already in bytes */
  274. fsoff = off9660 + h->data*h->blockSize;
  275. if(fsoff > startoff)
  276. vtFatal("fossil data starts after cd data");
  277. if(off9660 + (vlong)h->end*h->blockSize < endoff)
  278. vtFatal("fossil data ends before cd data");
  279. if(fsoff%h->blockSize)
  280. vtFatal("cd offset not a multiple of fossil block size");
  281. /* Read "same" block via CD image and via Fossil image */
  282. getsect(sect, startoff/Blocksize);
  283. if(seek(fd, startoff-off9660, 0) < 0)
  284. vtFatal("cannot seek to first data sector on cd via fossil");
  285. fprint(2, "look for %lud at %lud\n", startoff, startoff-off9660);
  286. if(readn(fd, sect2, Blocksize) != Blocksize)
  287. vtFatal("cannot read first data sector on cd via fossil");
  288. if(memcmp(sect, sect2, Blocksize) != 0)
  289. vtFatal("iso9660 offset is a lie %08ux %08ux", *(long*)sect, *(long*)sect2);
  290. }
  291. void
  292. iso9660labels(Disk *disk, uchar *buf, void (*write)(int, u32int))
  293. {
  294. ulong sb, eb, bn, lb, llb;
  295. Label l;
  296. int lpb;
  297. uchar sect[Blocksize];
  298. if(!diskReadRaw(disk, PartData, (startoff-fsoff)/h->blockSize, buf))
  299. vtFatal("disk read failed: %r");
  300. getsect(sect, startoff/Blocksize);
  301. if(memcmp(buf, sect, Blocksize) != 0)
  302. vtFatal("fsoff is wrong");
  303. sb = (startoff-fsoff)/h->blockSize;
  304. eb = (endoff-fsoff+h->blockSize-1)/h->blockSize;
  305. lpb = h->blockSize/LabelSize;
  306. /* for each reserved block, mark label */
  307. llb = ~0;
  308. l.type = BtData;
  309. l.state = BsAlloc;
  310. l.tag = Tag;
  311. l.epoch = 1;
  312. l.epochClose = ~(u32int)0;
  313. for(bn=sb; bn<eb; bn++){
  314. lb = bn/lpb;
  315. if(lb != llb){
  316. if(llb != ~0)
  317. (*write)(PartLabel, llb);
  318. memset(buf, 0, h->blockSize);
  319. }
  320. llb = lb;
  321. labelPack(&l, buf, bn%lpb);
  322. }
  323. if(llb != ~0)
  324. (*write)(PartLabel, llb);
  325. }
  326. void
  327. iso9660copy(Fs *fs)
  328. {
  329. File *root;
  330. root = fileOpen(fs, "/active");
  331. iso9660copydir(fs, root, (Cdir*)v->rootdir);
  332. fileDecRef(root);
  333. vtRUnlock(fs->elk);
  334. if(!fsSnapshot(fs, nil, nil, 0))
  335. vtFatal("snapshot failed: %R");
  336. vtRLock(fs->elk);
  337. }
  338. /*
  339. * The first block used is the first data block of the leftmost file in the tree.
  340. * (Just an artifact of how mk9660 works.)
  341. */
  342. static ulong
  343. iso9660start(Cdir *c)
  344. {
  345. uchar sect[Blocksize];
  346. while(c->flags&2){
  347. getsect(sect, little(c->dloc, 4));
  348. c = (Cdir*)sect;
  349. c = (Cdir*)((uchar*)c+c->len); /* skip dot */
  350. c = (Cdir*)((uchar*)c+c->len); /* skip dotdot */
  351. /* oops: might happen if leftmost directory is empty or leftmost file is zero length! */
  352. if(little(c->dloc, 4) == 0)
  353. vtFatal("error parsing cd image or unfortunate cd image");
  354. }
  355. return little(c->dloc, 4);
  356. }
  357. static void
  358. iso9660copydir(Fs *fs, File *dir, Cdir *cd)
  359. {
  360. ulong off, end, len;
  361. uchar sect[Blocksize], *esect, *p;
  362. Cdir *c;
  363. len = little(cd->dlen, 4);
  364. off = little(cd->dloc, 4)*Blocksize;
  365. end = off+len;
  366. esect = sect+Blocksize;
  367. for(; off<end; off+=Blocksize){
  368. getsect(sect, off/Blocksize);
  369. p = sect;
  370. while(p < esect){
  371. c = (Cdir*)p;
  372. if(c->len <= 0)
  373. break;
  374. if(c->namelen!=1 || c->name[0]>1)
  375. iso9660copyfile(fs, dir, c);
  376. p += c->len;
  377. }
  378. }
  379. }
  380. static char*
  381. getname(uchar **pp)
  382. {
  383. uchar *p;
  384. int l;
  385. p = *pp;
  386. l = *p;
  387. *pp = p+1+l;
  388. if(l == 0)
  389. return "";
  390. memmove(p, p+1, l);
  391. p[l] = 0;
  392. return (char*)p;
  393. }
  394. static char*
  395. getcname(Cdir *c)
  396. {
  397. uchar *up;
  398. char *p, *q;
  399. up = &c->namelen;
  400. p = getname(&up);
  401. for(q=p; *q; q++)
  402. *q = tolower(*q);
  403. return p;
  404. }
  405. static char
  406. dmsize[12] =
  407. {
  408. 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  409. };
  410. static ulong
  411. getcdate(uchar *p) /* yMdhmsz */
  412. {
  413. Tm tm;
  414. int y, M, d, h, m, s, tz;
  415. y=p[0]; M=p[1]; d=p[2];
  416. h=p[3]; m=p[4]; s=p[5]; tz=p[6];
  417. USED(tz);
  418. if (y < 70)
  419. return 0;
  420. if (M < 1 || M > 12)
  421. return 0;
  422. if (d < 1 || d > dmsize[M-1])
  423. return 0;
  424. if (h > 23)
  425. return 0;
  426. if (m > 59)
  427. return 0;
  428. if (s > 59)
  429. return 0;
  430. memset(&tm, 0, sizeof tm);
  431. tm.sec = s;
  432. tm.min = m;
  433. tm.hour = h;
  434. tm.mday = d;
  435. tm.mon = M-1;
  436. tm.year = 1900+y;
  437. tm.zone[0] = 0;
  438. return tm2sec(&tm);
  439. }
  440. static int ind;
  441. static void
  442. iso9660copyfile(Fs *fs, File *dir, Cdir *c)
  443. {
  444. Dir d;
  445. DirEntry de;
  446. int sysl;
  447. uchar score[VtScoreSize];
  448. ulong off, foff, len, mode;
  449. uchar *p;
  450. File *f;
  451. ind++;
  452. memset(&d, 0, sizeof d);
  453. p = c->name + c->namelen;
  454. if(((uintptr)p) & 1)
  455. p++;
  456. sysl = (uchar*)c + c->len - p;
  457. if(sysl <= 0)
  458. vtFatal("missing plan9 directory entry on %d/%d/%.*s", c->namelen, c->name[0], c->namelen, c->name);
  459. d.name = getname(&p);
  460. d.uid = getname(&p);
  461. d.gid = getname(&p);
  462. if((uintptr)p & 1)
  463. p++;
  464. d.mode = little(p, 4);
  465. if(d.name[0] == 0)
  466. d.name = getcname(c);
  467. d.mtime = getcdate(c->date);
  468. d.atime = d.mtime;
  469. if(d.mode&DMDIR) print("%*scopy %s %s %s %luo\n", ind*2, "", d.name, d.uid, d.gid, d.mode);
  470. mode = d.mode&0777;
  471. if(d.mode&DMDIR)
  472. mode |= ModeDir;
  473. if((f = fileCreate(dir, d.name, mode, d.uid)) == nil)
  474. vtFatal("could not create file '%s': %r", d.name);
  475. if(d.mode&DMDIR)
  476. iso9660copydir(fs, f, c);
  477. else{
  478. len = little(c->dlen, 4);
  479. off = little(c->dloc, 4)*Blocksize;
  480. for(foff=0; foff<len; foff+=h->blockSize){
  481. localToGlobal((off+foff-fsoff)/h->blockSize, score);
  482. if(!fileMapBlock(f, foff/h->blockSize, score, Tag))
  483. vtFatal("fileMapBlock: %R");
  484. }
  485. if(!fileSetSize(f, len))
  486. vtFatal("fileSetSize: %R");
  487. }
  488. if(!fileGetDir(f, &de))
  489. vtFatal("fileGetDir: %R");
  490. de.uid = d.uid;
  491. de.gid = d.gid;
  492. de.mtime = d.mtime;
  493. de.atime = d.atime;
  494. de.mode = d.mode&0777;
  495. if(!fileSetDir(f, &de, "sys"))
  496. vtFatal("fileSetDir: %R");
  497. fileDecRef(f);
  498. ind--;
  499. }