rd9660.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /*
  10. * dump a 9660 cd image for a little while.
  11. * for debugging.
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <bio.h>
  16. #include <disk.h>
  17. Biobuf *b;
  18. #pragma varargck type "s" uchar*
  19. #pragma varargck type "L" uchar*
  20. #pragma varargck type "B" uchar*
  21. #pragma varargck type "N" uchar*
  22. #pragma varargck type "T" uchar*
  23. #pragma varargck type "D" uchar*
  24. typedef struct Voldesc Voldesc;
  25. struct Voldesc {
  26. uint8_t magic[8]; /* 0x01, "CD001", 0x01, 0x00 */
  27. uint8_t systemid[32]; /* system identifier */
  28. uint8_t volumeid[32]; /* volume identifier */
  29. uint8_t unused[8]; /* character set in secondary desc */
  30. uint8_t volsize[8]; /* volume size */
  31. uint8_t charset[32];
  32. uint8_t volsetsize[4]; /* volume set size = 1 */
  33. uint8_t volseqnum[4]; /* volume sequence number = 1 */
  34. uint8_t blocksize[4]; /* logical block size */
  35. uint8_t pathsize[8]; /* path table size */
  36. uint8_t lpathloc[4]; /* Lpath */
  37. uint8_t olpathloc[4]; /* optional Lpath */
  38. uint8_t mpathloc[4]; /* Mpath */
  39. uint8_t ompathloc[4]; /* optional Mpath */
  40. uint8_t rootdir[34]; /* root directory */
  41. uint8_t volsetid[128]; /* volume set identifier */
  42. uint8_t publisher[128];
  43. uint8_t prepid[128]; /* data preparer identifier */
  44. uint8_t applid[128]; /* application identifier */
  45. uint8_t notice[37]; /* copyright notice file */
  46. uint8_t abstract[37]; /* abstract file */
  47. uint8_t biblio[37]; /* bibliographic file */
  48. uint8_t cdate[17]; /* creation date */
  49. uint8_t mdate[17]; /* modification date */
  50. uint8_t xdate[17]; /* expiration date */
  51. uint8_t edate[17]; /* effective date */
  52. uint8_t fsvers; /* file system version = 1 */
  53. };
  54. void
  55. dumpbootvol(void *a)
  56. {
  57. Voldesc *v;
  58. v = a;
  59. print("magic %.2ux %.5s %.2ux %2ux\n",
  60. v->magic[0], v->magic+1, v->magic[6], v->magic[7]);
  61. if(v->magic[0] == 0xFF)
  62. return;
  63. print("system %.32T\n", v->systemid);
  64. print("volume %.32T\n", v->volumeid);
  65. print("volume size %.4N\n", v->volsize);
  66. print("charset %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux %.2ux\n",
  67. v->charset[0], v->charset[1], v->charset[2], v->charset[3],
  68. v->charset[4], v->charset[5], v->charset[6], v->charset[7]);
  69. print("volume set size %.2N\n", v->volsetsize);
  70. print("volume sequence number %.2N\n", v->volseqnum);
  71. print("logical block size %.2N\n", v->blocksize);
  72. print("path size %.4L\n", v->pathsize);
  73. print("lpath loc %.4L\n", v->lpathloc);
  74. print("opt lpath loc %.4L\n", v->olpathloc);
  75. print("mpath loc %.4B\n", v->mpathloc);
  76. print("opt mpath loc %.4B\n", v->ompathloc);
  77. print("rootdir %D\n", v->rootdir);
  78. print("volume set identifier %.128T\n", v->volsetid);
  79. print("publisher %.128T\n", v->publisher);
  80. print("preparer %.128T\n", v->prepid);
  81. print("application %.128T\n", v->applid);
  82. print("notice %.37T\n", v->notice);
  83. print("abstract %.37T\n", v->abstract);
  84. print("biblio %.37T\n", v->biblio);
  85. print("creation date %.17s\n", v->cdate);
  86. print("modification date %.17s\n", v->mdate);
  87. print("expiration date %.17s\n", v->xdate);
  88. print("effective date %.17s\n", v->edate);
  89. print("fs version %d\n", v->fsvers);
  90. }
  91. typedef struct Cdir Cdir;
  92. struct Cdir {
  93. uint8_t len;
  94. uint8_t xlen;
  95. uint8_t dloc[8];
  96. uint8_t dlen[8];
  97. uint8_t date[7];
  98. uint8_t flags;
  99. uint8_t unitsize;
  100. uint8_t gapsize;
  101. uint8_t volseqnum[4];
  102. uint8_t namelen;
  103. uint8_t name[1]; /* chumminess */
  104. };
  105. #pragma varargck type "D" Cdir*
  106. int
  107. Dfmt(Fmt *fmt)
  108. {
  109. char buf[128];
  110. Cdir *c;
  111. c = va_arg(fmt->args, Cdir*);
  112. if(c->namelen == 1 && c->name[0] == '\0' || c->name[0] == '\001') {
  113. snprint(buf, sizeof buf, ".%s dloc %.4N dlen %.4N",
  114. c->name[0] ? "." : "", c->dloc, c->dlen);
  115. } else {
  116. snprint(buf, sizeof buf, "%.*T dloc %.4N dlen %.4N", c->namelen, c->name,
  117. c->dloc, c->dlen);
  118. }
  119. fmtstrcpy(fmt, buf);
  120. return 0;
  121. }
  122. typedef struct Path Path;
  123. struct Path {
  124. uint8_t namelen;
  125. uint8_t xlen;
  126. uint8_t dloc[4];
  127. uint8_t parent[2];
  128. uint8_t name[1]; /* chumminess */
  129. };
  130. #pragma varargck type "P" Path*
  131. int8_t longc, shortc;
  132. void
  133. bigend(void)
  134. {
  135. longc = 'B';
  136. }
  137. void
  138. littleend(void)
  139. {
  140. longc = 'L';
  141. }
  142. int
  143. Pfmt(Fmt *fmt)
  144. {
  145. char xfmt[128], buf[128];
  146. Path *p;
  147. p = va_arg(fmt->args, Path*);
  148. sprint(xfmt, "data=%%.4%c up=%%.2%c name=%%.*T (%%d)", longc, longc);
  149. snprint(buf, sizeof buf, xfmt, p->dloc, p->parent, p->namelen, p->name, p->namelen);
  150. fmtstrcpy(fmt, buf);
  151. return 0;
  152. }
  153. uint32_t
  154. big(void *a, int n)
  155. {
  156. uint8_t *p;
  157. uint32_t v;
  158. int i;
  159. p = a;
  160. v = 0;
  161. for(i=0; i<n; i++)
  162. v = (v<<8) | *p++;
  163. return v;
  164. }
  165. uint32_t
  166. little(void *a, int n)
  167. {
  168. uint8_t *p;
  169. uint32_t v;
  170. int i;
  171. p = a;
  172. v = 0;
  173. for(i=0; i<n; i++)
  174. v |= (*p++<<(i*8));
  175. return v;
  176. }
  177. /* numbers in big or little endian. */
  178. int
  179. BLfmt(Fmt *fmt)
  180. {
  181. uint32_t v;
  182. uint8_t *p;
  183. char buf[20];
  184. p = va_arg(fmt->args, uint8_t*);
  185. if(!(fmt->flags&FmtPrec)) {
  186. fmtstrcpy(fmt, "*BL*");
  187. return 0;
  188. }
  189. if(fmt->r == 'B')
  190. v = big(p, fmt->prec);
  191. else
  192. v = little(p, fmt->prec);
  193. sprint(buf, "0x%.*lux", fmt->prec*2, v);
  194. fmt->flags &= ~FmtPrec;
  195. fmtstrcpy(fmt, buf);
  196. return 0;
  197. }
  198. /* numbers in both little and big endian */
  199. int
  200. Nfmt(Fmt *fmt)
  201. {
  202. char buf[100];
  203. uint8_t *p;
  204. p = va_arg(fmt->args, uint8_t*);
  205. sprint(buf, "%.*L %.*B", fmt->prec, p, fmt->prec, p+fmt->prec);
  206. fmt->flags &= ~FmtPrec;
  207. fmtstrcpy(fmt, buf);
  208. return 0;
  209. }
  210. int
  211. asciiTfmt(Fmt *fmt)
  212. {
  213. char *p, buf[256];
  214. int i;
  215. p = va_arg(fmt->args, char*);
  216. for(i=0; i<fmt->prec; i++)
  217. buf[i] = *p++;
  218. buf[i] = '\0';
  219. for(p=buf+strlen(buf); p>buf && p[-1]==' '; p--)
  220. ;
  221. p[0] = '\0';
  222. fmt->flags &= ~FmtPrec;
  223. fmtstrcpy(fmt, buf);
  224. return 0;
  225. }
  226. int
  227. runeTfmt(Fmt *fmt)
  228. {
  229. Rune buf[256], *r;
  230. int i;
  231. uint8_t *p;
  232. p = va_arg(fmt->args, uint8_t*);
  233. for(i=0; i*2+2<=fmt->prec; i++, p+=2)
  234. buf[i] = (p[0]<<8)|p[1];
  235. buf[i] = L'\0';
  236. for(r=buf+i; r>buf && r[-1]==L' '; r--)
  237. ;
  238. r[0] = L'\0';
  239. fmt->flags &= ~FmtPrec;
  240. return fmtprint(fmt, "%S", buf);
  241. }
  242. void
  243. ascii(void)
  244. {
  245. fmtinstall('T', asciiTfmt);
  246. }
  247. void
  248. joliet(void)
  249. {
  250. fmtinstall('T', runeTfmt);
  251. }
  252. void
  253. getsect(uint8_t *buf, int n)
  254. {
  255. if(Bseek(b, n*2048, 0) != n*2048 || Bread(b, buf, 2048) != 2048)
  256. sysfatal("reading block %ux", n);
  257. }
  258. void
  259. pathtable(Voldesc *v, int islittle)
  260. {
  261. int i, j, n, sz, addr;
  262. uint32_t (*word)(void*, int);
  263. uint8_t x[2048], *p, *ep;
  264. Path *pt;
  265. print(">>> entire %s path table\n", islittle ? "little" : "big");
  266. littleend();
  267. if(islittle) {
  268. littleend();
  269. word = little;
  270. }else{
  271. bigend();
  272. word = big;
  273. }
  274. sz = little(v->pathsize, 4); /* little, not word */
  275. addr = word(islittle ? v->lpathloc : v->mpathloc, 4);
  276. j = 0;
  277. n = 1;
  278. while(sz > 0){
  279. getsect(x, addr);
  280. p = x;
  281. ep = x+sz;
  282. if(ep > x+2048)
  283. ep = x+2048;
  284. for(i=0; p<ep; i++) {
  285. pt = (Path*)p;
  286. if(pt->namelen==0)
  287. break;
  288. print("0x%.4x %4d+%-4ld %P\n", n, j, p-x, pt);
  289. n++;
  290. p += 8+pt->namelen+(pt->namelen&1);
  291. }
  292. sz -= 2048;
  293. addr++;
  294. j++;
  295. }
  296. }
  297. void
  298. dump(void *root)
  299. {
  300. Voldesc *v;
  301. Cdir *c;
  302. int32_t rootdirloc;
  303. uint8_t x[2048];
  304. int i;
  305. uint8_t *p;
  306. dumpbootvol(root);
  307. v = (Voldesc*)root;
  308. c = (Cdir*)v->rootdir;
  309. rootdirloc = little(c->dloc, 4);
  310. print(">>> sed 5q root directory\n");
  311. getsect(x, rootdirloc);
  312. p = x;
  313. for(i=0; i<5 && (p-x)<little(c->dlen, 4); i++) {
  314. print("%D\n", p);
  315. p += ((Cdir*)p)->len;
  316. }
  317. pathtable(v, 1);
  318. pathtable(v, 0);
  319. }
  320. void
  321. main(int argc, char **argv)
  322. {
  323. uint8_t root[2048], jroot[2048];
  324. if(argc != 2)
  325. sysfatal("usage: %s file", argv[0]);
  326. b = Bopen(argv[1], OREAD);
  327. if(b == nil)
  328. sysfatal("bopen %r");
  329. fmtinstall('L', BLfmt);
  330. fmtinstall('B', BLfmt);
  331. fmtinstall('N', Nfmt);
  332. fmtinstall('D', Dfmt);
  333. fmtinstall('P', Pfmt);
  334. getsect(root, 16);
  335. ascii();
  336. dump(root);
  337. getsect(jroot, 17);
  338. joliet();
  339. dump(jroot);
  340. exits(0);
  341. }