1
0

dnix-traverse.cpp 11 KB

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