fs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * aarch64-rpi/fs.h
  3. *
  4. * Copyright (C) 2017 - 2021 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * This file is part of the BOOTBOOT Protocol package.
  27. * @brief Filesystem drivers for initial ramdisk.
  28. *
  29. */
  30. /**
  31. * FS/Z initrd (OS/Z's native file system)
  32. */
  33. file_t fsz_initrd(unsigned char *initrd_p, char *kernel)
  34. {
  35. file_t ret = { NULL, 0 };
  36. if(initrd_p==NULL || memcmp(initrd_p + 512,"FS/Z",4) || kernel==NULL){
  37. return ret;
  38. }
  39. unsigned char passphrase[256],chk[32],iv[32];
  40. unsigned int i,j,k,l,ss=1<<(initrd_p[518]+11);
  41. unsigned char *ent, *in=(initrd_p+*((uint64_t*)(initrd_p+560))*ss);
  42. SHA256_CTX ctx;
  43. DBG(" * FS/Z ");
  44. DBG(kernel);
  45. DBG("\n");
  46. //decrypt initrd
  47. if(*((uint32_t*)(initrd_p+708))!=0 && (initrd_p[519]&0xF0)!=0) {
  48. puts("BOOTBOOT-PANIC: Unsupported cipher\n");
  49. return ret;
  50. }
  51. while(*((uint32_t*)(initrd_p+520))!=0) {
  52. puts(" * Passphrase? ");
  53. l=ReadLine(passphrase,sizeof(passphrase));
  54. if(!l) {
  55. puts("\n");
  56. return ret;
  57. }
  58. if(*((uint32_t*)(initrd_p+520))!=crc32_calc((char*)passphrase,l)) {
  59. puts("\rBOOTBOOT-ERROR: Bad passphrase\n");
  60. continue;
  61. }
  62. puts("\r * Decrypting...\r");
  63. SHA256_Init(&ctx);
  64. SHA256_Update(&ctx,passphrase,l);
  65. SHA256_Update(&ctx,initrd_p+512,6);
  66. SHA256_Final(chk,&ctx);
  67. for(i=0;i<32;i++) initrd_p[i+680]^=chk[i];
  68. SHA256_Init(&ctx);
  69. SHA256_Update(&ctx,initrd_p+680,32);
  70. SHA256_Final(iv,&ctx);
  71. // FSZ_SB_EALG_SHACBC
  72. for(k=ss,j=1;j<*((uint32_t*)(initrd_p+528));j++) {
  73. memcpy(chk,iv,32);
  74. for(i=0;i<ss;i++) {
  75. if(i%32==0) {
  76. SHA256_Init(&ctx);
  77. SHA256_Update(&ctx,&chk,32);
  78. SHA256_Update(&ctx,&j,4);
  79. SHA256_Final(chk,&ctx);
  80. }
  81. initrd_p[k++]^=chk[i%32]^iv[i%32];
  82. }
  83. }
  84. memset(initrd_p+680,0,32); *((uint32_t*)(initrd_p+520)) = 0;
  85. *((uint32_t*)(initrd_p+1020))=crc32_calc((char *)initrd_p+512,508);
  86. puts(" \r");
  87. }
  88. // Get the inode
  89. char *s,*e;
  90. s=e=kernel;
  91. i=0;
  92. again:
  93. while(*e!='/'&&*e!=0){e++;}
  94. if(*e=='/'){e++;}
  95. if(!memcmp(in,"FSIN",4)){
  96. //is it inlined?
  97. if(!memcmp(initrd_p[520]&1? in + 2048 : in + 1024,"FSDR",4)){
  98. ent=(initrd_p[520]&1? in + 2048 : in + 1024);
  99. } else if(!memcmp(initrd_p+*((uint64_t*)(in+448))*ss,"FSDR",4)){
  100. // go, get the sector pointed
  101. ent=(initrd_p+*((uint64_t*)(in+448))*ss);
  102. } else {
  103. return ret;
  104. }
  105. //skip header
  106. unsigned char *hdr=ent; ent+=128;
  107. //iterate on directory entries
  108. int j=*((uint64_t*)(hdr+16));
  109. while(j-->0){
  110. if(!memcmp(ent + 16,s,e-s)) {
  111. if(*e==0) {
  112. i=*((uint64_t*)(ent+0));
  113. break;
  114. } else {
  115. s=e;
  116. in=(initrd_p+*((uint64_t*)(ent+0))*ss);
  117. goto again;
  118. }
  119. }
  120. ent+=128;
  121. }
  122. } else {
  123. i=0;
  124. }
  125. if(i!=0) {
  126. // fid -> inode ptr -> data ptr
  127. unsigned char *in=(initrd_p+i*ss);
  128. if(!memcmp(in,"FSIN",4)){
  129. ret.size=*((uint64_t*)(in+464));
  130. if(*((uint64_t*)(in+448)) == i) {
  131. if(!(in[488]&31)) {
  132. // inline data
  133. ret.ptr=(uint8_t*)(initrd_p+i*ss+(initrd_p[520]&1? 2048 : 1024));
  134. } else {
  135. // sector directory or list inlined
  136. ret.ptr=(uint8_t*)(initrd_p + *((uint64_t*)(initrd_p[520]&1? in + 2048 : in + 1024))*ss);
  137. }
  138. } else
  139. if(*((uint64_t*)(in+448))) {
  140. switch((in[488]&15) + (in[488]&16 ? 1 : 0)) {
  141. case 0: // direct data
  142. ret.ptr=(uint8_t*)(initrd_p + *((uint64_t*)(in+448)) * ss);
  143. break;
  144. case 1: // sector directory or list (only one level supported here, and no holes in files)
  145. ret.ptr=(uint8_t*)(initrd_p + *((uint64_t*)(initrd_p + *((uint64_t*)(in+448))*ss)) * ss);
  146. break;
  147. }
  148. } else ret.size=0;
  149. }
  150. }
  151. return ret;
  152. }
  153. /**
  154. * Minix3 file system
  155. * directories only checked for their first block, and kernel must be defragmented
  156. */
  157. file_t mfs_initrd(unsigned char *initrd_p, char *kernel)
  158. {
  159. uint32_t o, bs, ino_tbl;
  160. uint8_t *ino, *d;
  161. char *s = kernel, *e;
  162. file_t ret = { NULL, 0 };
  163. if(initrd_p[1048] != 'Z' || initrd_p[1049] != 'M') return ret;
  164. DBG(" * MFS ");
  165. DBG(kernel);
  166. DBG("\n");
  167. bs = *((uint16_t*)(initrd_p + 1052));
  168. ino_tbl = (2 + *((uint16_t*)(initrd_p + 1030)) + *((uint16_t*)(initrd_p + 1032))) * bs;
  169. ino = initrd_p + ino_tbl;
  170. again:
  171. for(e = s; *e && *e != '/'; e++);
  172. d = initrd_p + *((uint32_t*)(ino + 24)) * bs;
  173. for(o = 0; o < *((uint32_t*)(ino + 8)) && o < bs; o += 64, d += 64) {
  174. if(*((uint32_t*)d) && !memcmp(s, d + 4, e - s) && !d[e - s]) {
  175. ino = initrd_p + ino_tbl + (*((uint32_t*)d) - 1) * 64;
  176. d = initrd_p + *((uint32_t*)(ino + 24)) * bs;
  177. if(!*e) { ret.ptr = d; ret.size = *((uint32_t*)(ino + 8)); return ret; }
  178. s = e + 1; goto again;
  179. }
  180. }
  181. return ret;
  182. }
  183. /**
  184. * cpio archive
  185. */
  186. file_t cpio_initrd(unsigned char *initrd_p, char *kernel)
  187. {
  188. unsigned char *ptr=initrd_p;
  189. int k;
  190. file_t ret = { NULL, 0 };
  191. if(initrd_p==NULL || kernel==NULL ||
  192. (memcmp(initrd_p,"070701",6) && memcmp(initrd_p,"070702",6) && memcmp(initrd_p,"070707",6)))
  193. return ret;
  194. DBG(" * cpio ");
  195. DBG(kernel);
  196. DBG("\n");
  197. k=strlen((unsigned char*)kernel);
  198. // hpodc archive
  199. while(!memcmp(ptr,"070707",6)){
  200. int ns=oct2bin(ptr+8*6+11,6);
  201. int fs=oct2bin(ptr+8*6+11+6,11);
  202. if(!memcmp(ptr+9*6+2*11,kernel,k+1) ||
  203. (ptr[9*6+2*11] == '.' && ptr[9*6+2*11+1] == '/' && !memcmp(ptr+9*6+2*11+2,kernel,k+1))) {
  204. ret.size=fs;
  205. ret.ptr=(uint8_t*)(ptr+9*6+2*11+ns);
  206. return ret;
  207. }
  208. ptr+=(76+ns+fs);
  209. }
  210. // newc and crc archive
  211. while(!memcmp(ptr,"07070",5)){
  212. int fs=hex2bin(ptr+8*6+6,8);
  213. int ns=hex2bin(ptr+8*11+6,8);
  214. if(!memcmp(ptr+110,kernel,k+1) || (ptr[110] == '.' && ptr[111] == '/' && !memcmp(ptr+112,kernel,k+1))) {
  215. ret.size=fs;
  216. ret.ptr=(uint8_t*)(ptr+((110+ns+3)/4)*4);
  217. return ret;
  218. }
  219. ptr+=((110+ns+3)/4)*4 + ((fs+3)/4)*4;
  220. }
  221. return ret;
  222. }
  223. /**
  224. * ustar tarball archive
  225. */
  226. file_t tar_initrd(unsigned char *initrd_p, char *kernel)
  227. {
  228. unsigned char *ptr=initrd_p;
  229. int k;
  230. file_t ret = { NULL, 0 };
  231. if(initrd_p==NULL || kernel==NULL || memcmp(initrd_p+257,"ustar",5))
  232. return ret;
  233. DBG(" * tar ");
  234. DBG(kernel);
  235. DBG("\n");
  236. k=strlen((unsigned char*)kernel);
  237. while(!memcmp(ptr+257,"ustar",5)){
  238. int fs=oct2bin(ptr+0x7c,11);
  239. if(!memcmp(ptr,kernel,k+1) || (ptr[0] == '.' && ptr[1] == '/' && !memcmp(ptr+2,kernel,k+1))) {
  240. ret.size=fs;
  241. ret.ptr=(uint8_t*)(ptr+512);
  242. return ret;
  243. }
  244. ptr+=(((fs+511)/512)+1)*512;
  245. }
  246. return ret;
  247. }
  248. /**
  249. * Simple File System
  250. */
  251. file_t sfs_initrd(unsigned char *initrd_p, char *kernel)
  252. {
  253. unsigned char *ptr, *end;
  254. int k,bs,ver;
  255. file_t ret = { NULL, 0 };
  256. if(initrd_p==NULL || kernel==NULL || (memcmp(initrd_p+0x1AC,"SFS",3) && memcmp(initrd_p+0x1A6,"SFS",3)))
  257. return ret;
  258. // 1.0 Brendan's version, 1.10 BenLunt's version
  259. ver=!memcmp(initrd_p+0x1A6,"SFS",3)?10:0;
  260. bs=1<<(7+(uint8_t)initrd_p[ver?0x1B6:0x1BC]);
  261. end=initrd_p + *((uint64_t *)&initrd_p[ver?0x1AA:0x1B0]) * bs; // base + total_number_of_blocks * blocksize
  262. // get index area
  263. ptr=end - *((uint64_t *)&initrd_p[ver?0x19E:0x1A4]); // end - size of index area
  264. // got a Starting Marker Entry?
  265. if(ptr[0]!=2)
  266. return ret;
  267. DBG(" * SFS 1.");
  268. DBG(ver?"10":"0");
  269. DBG(kernel);
  270. DBG("\n");
  271. k=strlen((unsigned char*)kernel);
  272. // iterate on index until we reach the end or Volume Identifier
  273. while(ptr<end && ptr[0]!=0x01){
  274. ptr+=64;
  275. // file entry?
  276. if(ptr[0]!=0x12)
  277. continue;
  278. // filename match?
  279. if(!memcmp(ptr+(ver?0x23:0x22),kernel,k+1)){
  280. ret.size=*((uint64_t*)&ptr[ver?0x1B:0x1A]); // file_length
  281. ret.ptr=initrd_p + *((uint64_t*)&ptr[ver?0x0B:0x0A]) * bs; // base + start_block * blocksize
  282. break;
  283. }
  284. }
  285. return ret;
  286. }
  287. /**
  288. * James Molloy's initrd (for some reason it's popular among hobby OS developers)
  289. * http://www.jamesmolloy.co.uk/tutorial_html
  290. */
  291. file_t jamesm_initrd(unsigned char *initrd_p, char *kernel)
  292. {
  293. unsigned char *ptr=initrd_p+4;
  294. int i,k,nf=*((int*)initrd_p);
  295. file_t ret = { NULL, 0 };
  296. // no real magic, so we assume initrd contains at least one file...
  297. if(initrd_p==NULL || kernel==NULL || initrd_p[2]!=0 || initrd_p[3]!=0 || initrd_p[4]!=0xBF)
  298. return ret;
  299. DBG(" * JamesM ");
  300. DBG(kernel);
  301. DBG("\n");
  302. k=strlen((unsigned char*)kernel);
  303. for(i=0;i<nf && ptr[0]==0xBF;i++) {
  304. if(!memcmp(ptr+1,kernel,k+1)){
  305. ret.ptr=*((uint32_t*)(ptr+65)) + initrd_p;
  306. ret.size=*((uint32_t*)(ptr+69));
  307. }
  308. ptr+=73;
  309. }
  310. return ret;
  311. }
  312. /**
  313. * EchFS
  314. * http://github.com/echfs/echfs
  315. */
  316. file_t ech_initrd(unsigned char *initrd_p, char *kernel)
  317. {
  318. uint64_t parent = 0xffffffffffffffffUL, n;
  319. unsigned char *ptr;
  320. char *end, *fn;
  321. int k = 0;
  322. file_t ret = { NULL, 0 };
  323. if(initrd_p==NULL || kernel==NULL || memcmp(initrd_p+4,"_ECH_FS_",8))
  324. return ret;
  325. DBG(" * EchFS ");
  326. DBG(kernel);
  327. DBG("\n");
  328. memcpy(&k, initrd_p + 28, 4);
  329. memcpy(&n, initrd_p + 12, 8);
  330. ptr = initrd_p + (((n * 8 + k - 1) / k) + 16) * k;
  331. for(end = fn = kernel; *end && *end != '/'; end++);
  332. while(*((uint64_t*)ptr)) {
  333. if(*((uint64_t*)ptr) == parent && !memcmp(ptr + 9, fn, end - fn) && !ptr[9 + end - fn]) {
  334. parent = *((uint64_t*)(ptr + 240));
  335. if(!*end) {
  336. ret.size=*((uint64_t*)(ptr + 248));
  337. ret.ptr=(uint8_t*)(initrd_p + parent * k);
  338. break;
  339. }
  340. end++;
  341. for(fn = end; *end && *end != '/'; end++);
  342. }
  343. ptr += 256;
  344. }
  345. return ret;
  346. }
  347. /**
  348. * Static file system drivers registry
  349. */
  350. file_t (*fsdrivers[]) (unsigned char *, char *) = {
  351. fsz_initrd,
  352. mfs_initrd,
  353. cpio_initrd,
  354. tar_initrd,
  355. sfs_initrd,
  356. jamesm_initrd,
  357. ech_initrd,
  358. NULL
  359. };