rd9660.c 7.5 KB

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