dnix-traverse.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <string.h>
  11. #include <limits.h> /* PATH_MAX */
  12. #include <errno.h>
  13. typedef unsigned int daddr_t;
  14. typedef int dnix_time_t;
  15. typedef int dnix_ino_t;
  16. #pragma pack(1)
  17. //#define daddr_t dnix_daddr_t
  18. #define time_t dnix_time_t
  19. #define ino_t dnix_ino_t
  20. #include "../dnix-headers/diskpar.h"
  21. #include "../dnix-headers/inode.h"
  22. #include "../dnix-headers/sysfile.h"
  23. #include "../dnix-headers/dir.h"
  24. #undef ino_t
  25. //#undef daddr_t
  26. #undef time_t
  27. //#define daddr_t daddr_t
  28. #define time_t time_t
  29. #include <sys/stat.h> /* mkdir(2) */
  30. #define swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
  31. #define swap16(num) (((num >> 8) & 0xff) | ((num << 8) & 0xff00))
  32. class DnixFs {
  33. struct sysfptr dnixPartitionInfo;
  34. struct sysfile sysfile;
  35. FILE * image;
  36. public:
  37. DnixFs();
  38. void init( FILE * image );
  39. bool readInode (int inumber, struct dinode * inode);
  40. };
  41. bool DnixFs:: readInode (int inumber, struct dinode * inode) {
  42. struct dinode dinode;
  43. char buffer[26];
  44. struct tm * tm_info;
  45. time_t tim;
  46. int i;
  47. printf ("inumber %04X\n", inumber);
  48. printf ("insiz %04X\n", sysfile.s_insiz);
  49. printf ("inadr %04X\n", sysfile.s_inadr);
  50. printf ("Address to read inode from %04lX\n", (inumber-1) * (sizeof dinode) + sysfile.s_inadr);
  51. if (((inumber-1) * (sizeof dinode) + sysfile.s_inadr) > sysfile.s_insiz) {
  52. printf ("******************* invalid inode address. Refusing. It is outide valid range.\n");
  53. return false;
  54. }
  55. fseek (image, (inumber-1) * (sizeof dinode) + sysfile.s_inadr, SEEK_SET);
  56. fread ( (void * ) &dinode, sizeof dinode, 1, image);
  57. inode->di_mode = swap16(dinode.di_mode);
  58. inode->di_nlink = swap16(dinode.di_nlink);
  59. inode->di_uid = swap16(dinode.di_uid);
  60. inode->di_gid = swap16(dinode.di_gid);
  61. inode->di_size = swap32(dinode.di_size);
  62. inode->di_atime = swap32(dinode.di_atime);
  63. inode->di_atime = swap32(dinode.di_atime);
  64. inode->di_mtime = swap32(dinode.di_mtime);
  65. inode->di_ctime = swap32(dinode.di_ctime);
  66. memcpy((void *) inode->di_addr, (void *) dinode.di_addr, 40);
  67. //for (i=0; i<40; i++) {
  68. // printf ("di_addr[%d] = %02X %02X\n", i, inode->di_addr[i], dinode.di_addr[i]);
  69. //}
  70. printf("mode and type of file %04X\n", inode->di_mode);
  71. printf("number of links to file %d\n", inode->di_nlink);
  72. printf("owner's user id %d\n", inode->di_uid);
  73. printf("owner's group id %d\n", inode->di_gid);
  74. printf("number of bytes in file %d\n", inode->di_size);
  75. tim = inode->di_atime;
  76. tm_info = localtime((time_t *) ( &tim));
  77. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  78. printf("time last accessed %s\n", buffer);
  79. tim = inode->di_mtime;
  80. tm_info = localtime((time_t *) ( &tim));
  81. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  82. printf("time last modified %s\n", buffer);
  83. tim = inode->di_ctime;
  84. tm_info = localtime((time_t *) ( &tim));
  85. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  86. printf("time inode last modified %s\n", buffer);
  87. return true;
  88. }
  89. void DnixFs::init(FILE * img) {
  90. char buffer[26];
  91. struct tm * tm_info;
  92. daddr_t superblock_address;
  93. struct sysfile s;
  94. time_t tim;
  95. image = img;
  96. // Read the partition info
  97. fseek (image, SYSFPTR, SEEK_SET);
  98. fread ( (void * ) &dnixPartitionInfo, 512, 1, image);
  99. tim = swap32(dnixPartitionInfo.timestamp);
  100. tm_info = localtime((time_t *) ( &tim));
  101. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  102. superblock_address = swap32(dnixPartitionInfo.vdsp);
  103. printf ("Pointer to volume descriptor block: %08X\n", superblock_address);
  104. printf ("1:s complement of previous item: %08X\n", swap32(dnixPartitionInfo.vdspn));
  105. printf ("Volume clean flag: %08X\n", swap32(dnixPartitionInfo.cleanfl));
  106. printf ("Timestamp %s\n", buffer);
  107. fseek (image, superblock_address, SEEK_SET);
  108. fread ( (void *) &s, sizeof sysfile, 1, image);
  109. memcpy ( (void *) &sysfile, (void *) &s, sizeof sysfile);
  110. sysfile.s_bmadr = swap32(s.s_bmadr);
  111. sysfile.s_inadr = swap32(s.s_inadr);
  112. sysfile.s_rtadr = swap32(s.s_rtadr);
  113. sysfile.s_swadr = swap32(s.s_swadr);
  114. sysfile.s_vlsiz = swap32(s.s_vlsiz);
  115. sysfile.s_bmsiz = swap32(s.s_bmsiz);
  116. sysfile.s_swsiz = swap32(s.s_swsiz);
  117. sysfile.s_bksiz = swap32(s.s_bksiz);
  118. sysfile.s_disiz = swap32(s.s_disiz);
  119. sysfile.s_insiz = swap32(s.s_insiz);
  120. sysfile.s_time = swap32(s.s_time);
  121. printf ("Bitmap pointer: %08X\n", sysfile.s_bmadr);
  122. printf ("Inode list pointer: %08X\n", sysfile.s_inadr);
  123. printf ("Root file pointer: %08X\n", sysfile.s_rtadr);
  124. printf ("Swap area: %08X\n", sysfile.s_swadr);
  125. printf ("Volume size: %d\n", sysfile.s_vlsiz);
  126. printf ("Bitmap size: %d\n", sysfile.s_bmsiz);
  127. printf ("Swap area size: %d\n", sysfile.s_swsiz);
  128. printf ("Block size: %d\n", sysfile.s_bksiz);
  129. printf ("Size of a directory entry: %d\n", sysfile.s_disiz);
  130. printf ("Default size of inode list: %08X\n", sysfile.s_insiz);
  131. tim = sysfile.s_time;
  132. tm_info = localtime((time_t *) ( &tim));
  133. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  134. printf ("Time initiated: %s\n", buffer);
  135. }
  136. // Read a file - block number as specified - store in buf
  137. class DnixFile {
  138. struct dinode ino;
  139. FILE * image;
  140. char indir_level_one [2048];
  141. int indir_level_one_block_no;
  142. char indir_level_two [2048];
  143. int indir_level_two_block_no;
  144. char indir_level_three [2048];
  145. int indir_level_three_block_no;
  146. public:
  147. void init( FILE * image, struct dinode * inode );
  148. void readFileBlock( int block_no, void * buf );
  149. };
  150. void DnixFile::init(FILE * img, struct dinode * inode) {
  151. memcpy (&ino, inode, sizeof (struct dinode));
  152. image = img;
  153. }
  154. void DnixFile::readFileBlock ( int block_no, void * buf ) {
  155. long disk_address;
  156. int ret;
  157. printf("block_no=%d\n",block_no );
  158. if (block_no<11) {
  159. // direct blocks
  160. disk_address = ((((long)(0xff& ino.di_addr[block_no*3])) <<16 ) | (((long)(0xff & ino.di_addr[block_no*3 + 1])) <<8 ) | (ino.di_addr[block_no*3 + 2] & 0xff))<<8;
  161. printf ("diskaddress=%08lX\n", disk_address);
  162. } else if ((block_no > 10) && (block_no < 692)) {
  163. // address of first indirect block is in block 11.
  164. disk_address = ((((long)(0xff& ino.di_addr[30])) <<16 ) | (((long)(0xff & ino.di_addr[31])) <<8 ) | (ino.di_addr[32] & 0xff))<<8;
  165. if (disk_address != indir_level_one_block_no) {
  166. fseek (image, disk_address, SEEK_SET);
  167. fread (indir_level_one, 2048, 1, image);
  168. }
  169. disk_address = ((((long)(0xff& indir_level_one[(block_no-10)*3])) <<16 ) | (((long)(0xff & indir_level_one[(block_no-10)*3 + 1])) <<8 ) | (indir_level_one[(block_no-10)*3 + 2] & 0xff))<<8;
  170. // First level of indirect block
  171. } else if ((block_no > 693) && (block_no < 465816)) {
  172. // Second level of indirect block
  173. disk_address = ((((long)(0xff& ino.di_addr[33])) <<16 ) | (((long)(0xff & ino.di_addr[34])) <<8 ) | (ino.di_addr[35] & 0xff))<<8;
  174. if (disk_address != indir_level_two_block_no) {
  175. fseek (image, disk_address, SEEK_SET);
  176. fread (indir_level_two, 2048, 1, image);
  177. indir_level_two_block_no = disk_address;
  178. }
  179. //disk_address = ((((long)(0xff& indir_level_two[(block_no-10)*3])) <<16 ) | (((long)(0xff & indir_level_one[(block_no-10)*3 + 1])) <<8 ) | (indir_level_one[(block_no-10)*3 + 2] & 0xff))<<8;
  180. //disk_address = indir_level_two[((block_no-693)/682)*3] * 65536 + indir_level_two[((block_no-693)/682)*3 + 1]*256 + indir_level_two[((block_no-693)/682)*3 + 2];
  181. if (disk_address != indir_level_one_block_no) {
  182. fseek (image, disk_address, SEEK_SET);
  183. fread (indir_level_one, 2048, 1, image);
  184. indir_level_one_block_no = disk_address;
  185. }
  186. disk_address = indir_level_one[(block_no-10)*3] * 65536 + indir_level_one[(block_no-10)*3 + 1]*256 + indir_level_one[(block_no-10)*3 + 2];
  187. } else {
  188. // Third level of indirect block
  189. disk_address = ino.di_addr[36] * 65536 + ino.di_addr[37]*256 + ino.di_addr[38];
  190. if (disk_address != indir_level_one_block_no) {
  191. fseek (image, disk_address, SEEK_SET);
  192. fread (indir_level_one, 2048, 1, image);
  193. }
  194. disk_address = ino.di_addr[(block_no-10)*3] * 65536 + ino.di_addr[(block_no-10)*3 + 1]*256 + ino.di_addr[(block_no-10)*3 + 2];
  195. }
  196. printf ("before fseek\n");
  197. ret = fseek (image, disk_address, SEEK_SET);
  198. printf ("fseek ret=%d\n", ret);
  199. ret = fread (buf, 2048, 1, image);
  200. printf ("fread ret=%d\n", ret);
  201. }
  202. DnixFs::DnixFs() {
  203. }
  204. int mkdir_p(const char *path)
  205. {
  206. /* Adapted from http://stackoverflow.com/a/2336245/119527 */
  207. const size_t len = strlen(path);
  208. char _path[PATH_MAX];
  209. char *p;
  210. errno = 0;
  211. /* Copy string so its mutable */
  212. if (len > sizeof(_path)-1) {
  213. errno = ENAMETOOLONG;
  214. return -1;
  215. }
  216. strcpy(_path, path);
  217. /* Iterate the string */
  218. for (p = _path + 1; *p; p++) {
  219. if (*p == '/') {
  220. /* Temporarily truncate */
  221. *p = '\0';
  222. if (mkdir(_path, S_IRWXU) != 0) {
  223. if (errno != EEXIST)
  224. return -1;
  225. }
  226. *p = '/';
  227. }
  228. }
  229. if (mkdir(_path, S_IRWXU) != 0) {
  230. if (errno != EEXIST)
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. void readDir(int inumber, FILE * image_file, class DnixFs * dnixFs, char * path) {
  236. char p [1024];
  237. const char * slash = "/";
  238. struct direct * dir;
  239. class DnixFile * file = new class DnixFile;
  240. int size;
  241. FILE *output;
  242. int block_no;
  243. int dir_cnt;
  244. bool ret;
  245. struct dinode inode;
  246. printf("Processing path=%s\n", path);
  247. ret = dnixFs->readInode(inumber, &inode);
  248. if (!ret) return;
  249. dir = (struct direct *) malloc (2048);
  250. file->init(image_file, &inode);
  251. size = inode.di_size;
  252. printf ("size=%d\n", size);
  253. if (inode.di_mode & 0x4000) {
  254. mkdir_p(path);
  255. // Directory
  256. // Allocate memory
  257. block_no = 0;
  258. do {
  259. file->readFileBlock(block_no, (void * ) dir);
  260. // process all the entries in the directory
  261. dir_cnt=0;
  262. do {
  263. printf ("inode: %d name: %s \n", swap16(dir[dir_cnt].d_ino), dir[dir_cnt].d_name);
  264. if (((strncmp(dir[dir_cnt].d_name,".",1)!=0) && (strncmp(dir[dir_cnt].d_name,"..",2)!=0)) || (strlen(dir[dir_cnt].d_name) > 2)) {
  265. memset(p,0,sizeof p);
  266. strcpy(p, path);
  267. strcat(p, slash);
  268. strncat(p, dir[dir_cnt].d_name,14);
  269. readDir(swap16(dir[dir_cnt].d_ino), image_file, dnixFs, p);
  270. }
  271. dir_cnt++;
  272. printf("dir_cnt=%d\n", dir_cnt);
  273. } while ((dir_cnt < 128) && (swap16(dir[dir_cnt].d_ino) != 0));
  274. size -= 2048;
  275. block_no ++;
  276. } while (size > 0 && swap16(dir[255].d_ino) !=0 );
  277. } else {
  278. // Ordinary file
  279. output = fopen (path, "w");
  280. chmod (path, inode.di_mode & 07777);
  281. chown (path, inode.di_gid, inode.di_uid);
  282. do {
  283. file->readFileBlock(block_no, (void * ) dir);
  284. if (size >= 2048) {
  285. fwrite(dir, 1, 2048, output);
  286. } else {
  287. fwrite(dir, 1, size, output);
  288. }
  289. size -= 2048;
  290. block_no ++;
  291. } while (size > 0);
  292. fclose(output);
  293. }
  294. }
  295. int main (int argc, char ** argv) {
  296. FILE * image_file;
  297. int opt;
  298. class DnixFs dnixFs;
  299. while ((opt = getopt(argc, argv, "d:")) != -1) {
  300. switch (opt) {
  301. case 'd':
  302. image_file = fopen (optarg, "r");
  303. if (image_file == NULL) {
  304. perror ("Failure opening file.");
  305. exit(EXIT_FAILURE);
  306. }
  307. break;
  308. default: /* '?' */
  309. fprintf(stderr, "Usage: %s [-d disk-image-file]\n", argv[0]);
  310. exit(EXIT_FAILURE);
  311. }
  312. }
  313. dnixFs.init(image_file);
  314. readDir(INOROOT, image_file, &dnixFs, (char *) ".");
  315. }