1
0

dnix-traverse.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. compile using : c++ dnix-traverse.cpp -o dnix-traverse
  3. Program to list and extract files from a DNIX FS. Should also be able to write to a DNIX file system. Possibly even add boot code and other things.
  4. */
  5. #ifdef __linux__
  6. #define _POSIX_SOURCE 1
  7. #endif
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <string.h>
  14. #include <limits.h> /* PATH_MAX */
  15. #include <errno.h>
  16. typedef unsigned int dnix_daddr_t;
  17. typedef int dnix_time_t;
  18. typedef int dnix_ino_t;
  19. #pragma pack(1)
  20. #define daddr_t dnix_daddr_t
  21. #define time_t dnix_time_t
  22. #define ino_t dnix_ino_t
  23. #include "../dnix-headers/diskpar.h"
  24. #include "../dnix-headers/inode.h"
  25. #include "../dnix-headers/sysfile.h"
  26. #include "../dnix-headers/dir.h"
  27. #undef ino_t
  28. #undef time_t
  29. #ifdef __linux__
  30. #undef daddr_t
  31. #endif
  32. #define time_t time_t
  33. #include <sys/stat.h> /* mkdir(2) */
  34. #include <utime.h>
  35. #define swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
  36. #define swap16(num) (((num >> 8) & 0xff) | ((num << 8) & 0xff00))
  37. bool sequential = false;
  38. int fseekWrapped (FILE * file, long pos, int type) {
  39. if (!sequential) {
  40. int track;
  41. int head = 0;
  42. int byte = (pos & 0xfff);
  43. track = (pos & 0xff000) >> 12;
  44. if (track>=80) {
  45. head = 1;
  46. track-=80;
  47. }
  48. pos = byte | (head << 12) | (track << 13);
  49. }
  50. return fseek(file, pos, type);
  51. }
  52. class DnixFs {
  53. struct sysfptr dnixPartitionInfo;
  54. struct sysfile sysfile;
  55. FILE * image;
  56. public:
  57. DnixFs();
  58. void init( FILE * image );
  59. bool readInode (int inumber, struct dinode * inode);
  60. int getVolumeSize ();
  61. int getBlockSize();
  62. };
  63. bool DnixFs:: readInode (int inumber, struct dinode * inode) {
  64. struct dinode dinode;
  65. char buffer[26];
  66. struct tm * tm_info;
  67. time_t tim;
  68. int i;
  69. if (((inumber-1) * (sizeof dinode) + sysfile.s_inadr) > sysfile.s_insiz) {
  70. fprintf(stderr, "inode=%d is outside the inode area.\n", inumber);
  71. return false;
  72. }
  73. fprintf(stderr, "Diskaddress = %08lX\n",(inumber-1) * (sizeof dinode) + sysfile.s_inadr);
  74. fseekWrapped (image, (inumber-1) * (sizeof dinode) + sysfile.s_inadr, SEEK_SET);
  75. fread ( (void * ) &dinode, sizeof dinode, 1, image);
  76. inode->di_mode = swap16(dinode.di_mode);
  77. inode->di_nlink = swap16(dinode.di_nlink);
  78. inode->di_uid = swap16(dinode.di_uid);
  79. inode->di_gid = swap16(dinode.di_gid);
  80. inode->di_size = swap32(dinode.di_size);
  81. inode->di_atime = swap32(dinode.di_atime);
  82. inode->di_mtime = swap32(dinode.di_mtime);
  83. inode->di_ctime = swap32(dinode.di_ctime);
  84. memcpy((void *) inode->di_addr, (void *) dinode.di_addr, 40);
  85. tim = inode->di_atime;
  86. tm_info = localtime((time_t *) ( &tim));
  87. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  88. tim = inode->di_mtime;
  89. tm_info = localtime((time_t *) ( &tim));
  90. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  91. tim = inode->di_ctime;
  92. tm_info = localtime((time_t *) ( &tim));
  93. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  94. return true;
  95. }
  96. int DnixFs::getVolumeSize() {
  97. return sysfile.s_vlsiz;
  98. }
  99. int DnixFs::getBlockSize() {
  100. return sysfile.s_bksiz;
  101. }
  102. void DnixFs::init(FILE * img) {
  103. char buffer[26];
  104. struct tm * tm_info;
  105. daddr_t superblock_address;
  106. struct sysfile s;
  107. time_t tim;
  108. image = img;
  109. // Read the partition info
  110. fseekWrapped (image, SYSFPTR, SEEK_SET);
  111. fread ( (void * ) &dnixPartitionInfo, 512, 1, image);
  112. tim = swap32(dnixPartitionInfo.timestamp);
  113. tm_info = localtime((time_t *) ( &tim));
  114. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  115. superblock_address = swap32(dnixPartitionInfo.vdsp);
  116. fseekWrapped (image, superblock_address, SEEK_SET);
  117. fread ( (void *) &s, sizeof sysfile, 1, image);
  118. memcpy ( (void *) &sysfile, (void *) &s, sizeof sysfile);
  119. sysfile.s_bmadr = swap32(s.s_bmadr);
  120. sysfile.s_inadr = swap32(s.s_inadr);
  121. sysfile.s_rtadr = swap32(s.s_rtadr);
  122. sysfile.s_swadr = swap32(s.s_swadr);
  123. sysfile.s_vlsiz = swap32(s.s_vlsiz);
  124. sysfile.s_bmsiz = swap32(s.s_bmsiz);
  125. sysfile.s_swsiz = swap32(s.s_swsiz);
  126. sysfile.s_bksiz = swap32(s.s_bksiz);
  127. sysfile.s_disiz = swap32(s.s_disiz);
  128. sysfile.s_insiz = swap32(s.s_insiz);
  129. sysfile.s_time = swap32(s.s_time);
  130. fprintf(stderr, "sysfile.s_inadr=%08X\n",sysfile.s_inadr);
  131. fprintf(stderr, "sysfile.s_insiz=%08X\n",sysfile.s_insiz);
  132. fprintf(stderr, "sysfile.s_bmsiz=%08X\n",sysfile.s_bmsiz);
  133. fprintf(stderr, "sysfile.s_disiz=%08X\n",sysfile.s_disiz);
  134. fprintf(stderr, "sysfile.s_vlsiz=%08X\n",sysfile.s_vlsiz);
  135. tim = sysfile.s_time;
  136. tm_info = localtime((time_t *) ( &tim));
  137. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  138. }
  139. // Read a file - block number as specified - store in buf
  140. struct BlockCache {
  141. int disk_address;
  142. int block[512];
  143. };
  144. class DnixFile {
  145. struct dinode ino;
  146. FILE * image;
  147. int volumeSize;
  148. int blockSize;
  149. struct BlockCache blockCache [3];
  150. void updateBlockCache(long disk_address, int level);
  151. public:
  152. void init( FILE * image, struct dinode * inode, int size, int blockSize);
  153. bool readFileBlock( int block_no, void * buf);
  154. };
  155. void DnixFile::updateBlockCache(long disk_address, int level) {
  156. if (disk_address != blockCache[level].disk_address) {
  157. fseekWrapped (image, disk_address, SEEK_SET);
  158. fread (blockCache[level].block, 2048, 1, image);
  159. }
  160. }
  161. void DnixFile::init(FILE * img, struct dinode * inode, int size, int bSz ) {
  162. memcpy (&ino, inode, sizeof (struct dinode));
  163. image = img;
  164. volumeSize = size;
  165. blockSize = bSz;
  166. }
  167. bool DnixFile::readFileBlock ( int block_no, void * buf ) {
  168. long disk_address;
  169. if (block_no<11) {
  170. // direct blocks
  171. disk_address = (((0xff& ino.di_addr[block_no*3]) <<16 ) | ((0xff & ino.di_addr[block_no*3 + 1]) <<8 ) | (ino.di_addr[block_no*3 + 2] & 0xff)) <<8;
  172. if (disk_address > volumeSize) return false;
  173. } else if ((block_no > 10) && (block_no < 522)) {
  174. disk_address = (((0xff& ino.di_addr[30]) <<16 ) | ((0xff & ino.di_addr[31]) <<8 ) | (ino.di_addr[32] & 0xff))<<8;
  175. if (disk_address > volumeSize) return false;
  176. updateBlockCache(disk_address, 0);
  177. disk_address = swap32(blockCache[0].block[block_no-11]);
  178. if (disk_address > volumeSize) return false;
  179. } else if ((block_no >= 522) && (block_no < 262666)) {
  180. // Second level of indirect block
  181. disk_address = (((0xff& ino.di_addr[33]) <<16 ) | ((0xff & ino.di_addr[34]) <<8 ) | (ino.di_addr[35] & 0xff))<<8;
  182. if (disk_address > volumeSize) return false;
  183. updateBlockCache(disk_address, 1);
  184. disk_address = swap32(blockCache[1].block[(block_no-522)>>9]);
  185. if (disk_address > volumeSize) return false;
  186. updateBlockCache(disk_address, 0);
  187. disk_address = swap32(blockCache[0].block[(block_no-522) & 0x1ff]);
  188. if (disk_address > volumeSize) return false;
  189. } else {
  190. // Third level of indirect block
  191. disk_address = (((0xff& ino.di_addr[36]) <<16 ) | ((0xff & ino.di_addr[37]) <<8 ) | (ino.di_addr[38] & 0xff))<<8;
  192. if (disk_address > volumeSize) return false;
  193. updateBlockCache(disk_address, 2);
  194. disk_address = swap32(blockCache[2].block[(block_no-262666) >> 18]);
  195. if (disk_address > volumeSize) return false;
  196. updateBlockCache(disk_address, 1);
  197. disk_address = swap32(blockCache[1].block[((block_no-262666) >>9) & 0x1ff]);
  198. if (disk_address > volumeSize) return false;
  199. updateBlockCache(disk_address, 0);
  200. disk_address = swap32(blockCache[0].block[(block_no-262266) & 0x1ff]);
  201. if (disk_address > volumeSize) return false;
  202. }
  203. fprintf(stderr, "readFileBlock disk_address=%08lX\n", disk_address);
  204. fseekWrapped (image, disk_address, SEEK_SET);
  205. fread (buf, blockSize, 1, image);
  206. return true;
  207. }
  208. DnixFs::DnixFs() {
  209. }
  210. int mkdir_p(const char *path)
  211. {
  212. /* Adapted from http://stackoverflow.com/a/2336245/119527 */
  213. const size_t len = strlen(path);
  214. char _path[PATH_MAX];
  215. char *p;
  216. errno = 0;
  217. /* Copy string so its mutable */
  218. if (len > sizeof(_path)-1) {
  219. errno = ENAMETOOLONG;
  220. return -1;
  221. }
  222. strcpy(_path, path);
  223. /* Iterate the string */
  224. for (p = _path + 1; *p; p++) {
  225. if (*p == '/') {
  226. /* Temporarily truncate */
  227. *p = '\0';
  228. if (mkdir(_path, S_IRWXU) != 0) {
  229. if (errno != EEXIST)
  230. return -1;
  231. }
  232. *p = '/';
  233. }
  234. }
  235. if (mkdir(_path, S_IRWXU) != 0) {
  236. if (errno != EEXIST)
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. void readDir(int inumber, FILE * image_file, class DnixFs * dnixFs, char * path, int recursionCount) {
  242. char p [1024];
  243. const char * slash = "/";
  244. struct direct * dir;
  245. char * buf;
  246. class DnixFile * file = new class DnixFile;
  247. int size;
  248. FILE *output;
  249. int block_no=0;
  250. int dir_cnt;
  251. bool ret;
  252. struct utimbuf timebuf;
  253. struct dinode inode;
  254. if (recursionCount > 64) {
  255. fprintf(stderr, "Too many levels of directories. Likely due to a damaged file system. Aborting..");
  256. exit(1);
  257. }
  258. fprintf(stderr, "Reading inumber=%d\n", inumber);
  259. ret = dnixFs->readInode(inumber, &inode);
  260. if (!ret) return;
  261. dir = (struct direct *) malloc (2048);
  262. file->init(image_file, &inode, dnixFs->getVolumeSize(), dnixFs->getBlockSize());
  263. size = inode.di_size;
  264. if (inode.di_mode & 0x4000) {
  265. mkdir_p(path);
  266. block_no = 0;
  267. do {
  268. ret=file->readFileBlock(block_no, (void * ) dir);
  269. if (!ret) {
  270. fprintf(stderr, "Error reading filesystem. Tried to read block address outside volume.\n");
  271. } else {
  272. dir_cnt=0;
  273. do {
  274. if (((strncmp(dir[dir_cnt].d_name,".",1)!=0) && (strncmp(dir[dir_cnt].d_name,"..",2)!=0)) || (strlen(dir[dir_cnt].d_name) > 2)) {
  275. memset(p,0,sizeof p);
  276. strcpy(p, path);
  277. fprintf(stderr, "Path=%s\n", path);
  278. strcat(p, slash);
  279. strncat(p, dir[dir_cnt].d_name,14);
  280. readDir(swap16(dir[dir_cnt].d_ino), image_file, dnixFs, p, recursionCount+1);
  281. }
  282. dir_cnt++;
  283. } while ((dir_cnt < 128) && (swap16(dir[dir_cnt].d_ino) != 0));
  284. }
  285. size -= 2048;
  286. block_no ++;
  287. } while (size > 0 && swap16(dir[255].d_ino) !=0 );
  288. } else {
  289. // Ordinary file
  290. output = fopen (path, "w");
  291. buf = (char *) malloc(dnixFs->getBlockSize());
  292. do {
  293. ret=file->readFileBlock(block_no, (void *) buf);
  294. if (!ret) {
  295. fprintf(stderr, "Error reading filesystem. Tried to read block address outside volume.\n");
  296. } else {
  297. if (size >= dnixFs->getBlockSize()) {
  298. fwrite(buf, 1, dnixFs->getBlockSize(), output);
  299. } else {
  300. fwrite(buf, 1, size, output);
  301. }
  302. }
  303. size -= dnixFs->getBlockSize();
  304. block_no ++;
  305. } while (size > 0);
  306. free(buf);
  307. fclose(output);
  308. }
  309. chmod (path, inode.di_mode & 07777);
  310. chown (path, inode.di_gid, inode.di_uid);
  311. timebuf.actime = inode.di_atime;
  312. timebuf.modtime = inode.di_mtime;
  313. utime(path, &timebuf);
  314. delete file;
  315. }
  316. int main (int argc, char ** argv) {
  317. FILE * image_file;
  318. int opt;
  319. class DnixFs dnixFs;
  320. while ((opt = getopt(argc, argv, "d:s")) != -1) {
  321. switch (opt) {
  322. case 'd':
  323. image_file = fopen (optarg, "r");
  324. if (image_file == NULL) {
  325. perror ("Failure opening file.");
  326. exit(EXIT_FAILURE);
  327. }
  328. break;
  329. case 's':
  330. sequential = true;
  331. break;
  332. default: /* '?' */
  333. fprintf(stderr, "Usage: %s [-d disk-image-file]\n", argv[0]);
  334. exit(EXIT_FAILURE);
  335. }
  336. }
  337. dnixFs.init(image_file);
  338. readDir(INOROOT, image_file, &dnixFs, (char *) ".",0);
  339. }