part_amiga.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * (C) Copyright 2001
  3. * Hans-Joerg Frieden, Hyperion Entertainment
  4. * Hans-JoergF@hyperion-entertainment.com
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <ide.h>
  27. #include "part_amiga.h"
  28. #ifdef HAVE_BLOCK_DEVICE
  29. #undef AMIGA_DEBUG
  30. #ifdef AMIGA_DEBUG
  31. #define PRINTF(fmt, args...) printf(fmt ,##args)
  32. #else
  33. #define PRINTF(fmt, args...)
  34. #endif
  35. struct block_header
  36. {
  37. u32 id;
  38. u32 summed_longs;
  39. s32 chk_sum;
  40. };
  41. static unsigned char block_buffer[DEFAULT_SECTOR_SIZE];
  42. static struct rigid_disk_block rdb = {0};
  43. static struct bootcode_block bootcode = {0};
  44. /*
  45. * Copy a bcpl to a c string
  46. */
  47. static void bcpl_strcpy(char *to, char *from)
  48. {
  49. int len = *from++;
  50. while (len)
  51. {
  52. *to++ = *from++;
  53. len--;
  54. }
  55. *to = 0;
  56. }
  57. /*
  58. * Print a BCPL String. BCPL strings start with a byte with the length
  59. * of the string, and don't contain a terminating nul character
  60. */
  61. static void bstr_print(char *string)
  62. {
  63. int len = *string++;
  64. char buffer[256];
  65. int i;
  66. i = 0;
  67. while (len)
  68. {
  69. buffer[i++] = *string++;
  70. len--;
  71. }
  72. buffer[i] = 0;
  73. printf("%-10s", buffer);
  74. }
  75. /*
  76. * Sum a block. The checksum of a block must end up at zero
  77. * to be valid. The chk_sum field is selected so that adding
  78. * it yields zero.
  79. */
  80. int sum_block(struct block_header *header)
  81. {
  82. s32 *block = (s32 *)header;
  83. u32 i;
  84. s32 sum = 0;
  85. for (i = 0; i < header->summed_longs; i++)
  86. sum += *block++;
  87. return (sum != 0);
  88. }
  89. /*
  90. * Print an AmigaOS disk type. Disk types are a four-byte identifier
  91. * describing the file system. They are usually written as a three-letter
  92. * word followed by a backslash and a version number. For example,
  93. * DOS\0 would be the original file system. SFS\0 would be SmartFileSystem.
  94. * DOS\1 is FFS.
  95. */
  96. static void print_disk_type(u32 disk_type)
  97. {
  98. char buffer[6];
  99. buffer[0] = (disk_type & 0xFF000000)>>24;
  100. buffer[1] = (disk_type & 0x00FF0000)>>16;
  101. buffer[2] = (disk_type & 0x0000FF00)>>8;
  102. buffer[3] = '\\';
  103. buffer[4] = (disk_type & 0x000000FF) + '0';
  104. buffer[5] = 0;
  105. printf("%s", buffer);
  106. }
  107. /*
  108. * Print the info contained within the given partition block
  109. */
  110. static void print_part_info(struct partition_block *p)
  111. {
  112. struct amiga_part_geometry *g;
  113. g = (struct amiga_part_geometry *)&(p->environment);
  114. bstr_print(p->drive_name);
  115. printf("%6d\t%6d\t",
  116. g->low_cyl * g->block_per_track * g->surfaces ,
  117. (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1);
  118. print_disk_type(g->dos_type);
  119. printf("\t%5d\n", g->boot_priority);
  120. }
  121. /*
  122. * Search for the Rigid Disk Block. The rigid disk block is required
  123. * to be within the first 16 blocks of a drive, needs to have
  124. * the ID AMIGA_ID_RDISK ('RDSK') and needs to have a valid
  125. * sum-to-zero checksum
  126. */
  127. struct rigid_disk_block *get_rdisk(block_dev_desc_t *dev_desc)
  128. {
  129. int i;
  130. int limit;
  131. char *s;
  132. s = getenv("amiga_scanlimit");
  133. if (s)
  134. limit = simple_strtoul(s, NULL, 10);
  135. else
  136. limit = AMIGA_BLOCK_LIMIT;
  137. for (i=0; i<limit; i++)
  138. {
  139. ulong res = dev_desc->block_read(dev_desc->dev, i, 1,
  140. (ulong *)block_buffer);
  141. if (res == 1)
  142. {
  143. struct rigid_disk_block *trdb = (struct rigid_disk_block *)block_buffer;
  144. if (trdb->id == AMIGA_ID_RDISK)
  145. {
  146. PRINTF("Rigid disk block suspect at %d, checking checksum\n",i);
  147. if (sum_block((struct block_header *)block_buffer) == 0)
  148. {
  149. PRINTF("FOUND\n");
  150. memcpy(&rdb, trdb, sizeof(struct rigid_disk_block));
  151. return (struct rigid_disk_block *)&rdb;
  152. }
  153. }
  154. }
  155. }
  156. PRINTF("Done scanning, no RDB found\n");
  157. return NULL;
  158. }
  159. /*
  160. * Search for boot code
  161. * Again, the first boot block must be located somewhere in the first 16 blocks, or rooted in the
  162. * Ridgid disk block
  163. */
  164. struct bootcode_block *get_bootcode(block_dev_desc_t *dev_desc)
  165. {
  166. int i;
  167. int limit;
  168. char *s;
  169. s = getenv("amiga_scanlimit");
  170. if (s)
  171. limit = simple_strtoul(s, NULL, 10);
  172. else
  173. limit = AMIGA_BLOCK_LIMIT;
  174. PRINTF("Scanning for BOOT from 0 to %d\n", limit);
  175. for (i = 0; i < limit; i++)
  176. {
  177. ulong res = dev_desc->block_read(dev_desc->dev, i, 1, (ulong *)block_buffer);
  178. if (res == 1)
  179. {
  180. struct bootcode_block *boot = (struct bootcode_block *)block_buffer;
  181. if (boot->id == AMIGA_ID_BOOT)
  182. {
  183. PRINTF("BOOT block at %d, checking checksum\n", i);
  184. if (sum_block((struct block_header *)block_buffer) == 0)
  185. {
  186. PRINTF("Found valid bootcode block\n");
  187. memcpy(&bootcode, boot, sizeof(struct bootcode_block));
  188. return &bootcode;
  189. }
  190. }
  191. }
  192. }
  193. PRINTF("No boot code found on disk\n");
  194. return 0;
  195. }
  196. /*
  197. * Test if the given partition has an Amiga partition table/Rigid
  198. * Disk block
  199. */
  200. int test_part_amiga(block_dev_desc_t *dev_desc)
  201. {
  202. struct rigid_disk_block *rdb;
  203. struct bootcode_block *bootcode;
  204. PRINTF("test_part_amiga: Testing for an Amiga RDB partition\n");
  205. rdb = get_rdisk(dev_desc);
  206. if (rdb)
  207. {
  208. bootcode = get_bootcode(dev_desc);
  209. if (bootcode)
  210. PRINTF("test_part_amiga: bootable Amiga disk\n");
  211. else
  212. PRINTF("test_part_amiga: non-bootable Amiga disk\n");
  213. return 0;
  214. }
  215. else
  216. {
  217. PRINTF("test_part_amiga: no RDB found\n");
  218. return -1;
  219. }
  220. }
  221. /*
  222. * Find partition number partnum on the given drive.
  223. */
  224. static struct partition_block *find_partition(block_dev_desc_t *dev_desc, int partnum)
  225. {
  226. struct rigid_disk_block *rdb;
  227. struct partition_block *p;
  228. u32 block;
  229. PRINTF("Trying to find partition block %d\n", partnum);
  230. rdb = get_rdisk(dev_desc);
  231. if (!rdb)
  232. {
  233. PRINTF("find_partition: no rdb found\n");
  234. return NULL;
  235. }
  236. PRINTF("find_partition: Scanning partition list\n");
  237. block = rdb->partition_list;
  238. PRINTF("find_partition: partition list at 0x%x\n", block);
  239. while (block != 0xFFFFFFFF)
  240. {
  241. ulong res = dev_desc->block_read(dev_desc->dev, block, 1,
  242. (ulong *)block_buffer);
  243. if (res == 1)
  244. {
  245. p = (struct partition_block *)block_buffer;
  246. if (p->id == AMIGA_ID_PART)
  247. {
  248. PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
  249. if (sum_block((struct block_header *)p) == 0)
  250. {
  251. if (partnum == 0) break;
  252. else
  253. {
  254. partnum--;
  255. block = p->next;
  256. }
  257. }
  258. } else block = 0xFFFFFFFF;
  259. } else block = 0xFFFFFFFF;
  260. }
  261. if (block == 0xFFFFFFFF)
  262. {
  263. PRINTF("PART block not found\n");
  264. return NULL;
  265. }
  266. return (struct partition_block *)block_buffer;
  267. }
  268. /*
  269. * Get info about a partition
  270. */
  271. int get_partition_info_amiga (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
  272. {
  273. struct partition_block *p = find_partition(dev_desc, part-1);
  274. struct amiga_part_geometry *g;
  275. u32 disk_type;
  276. if (!p) return -1;
  277. g = (struct amiga_part_geometry *)&(p->environment);
  278. info->start = g->low_cyl * g->block_per_track * g->surfaces;
  279. info->size = (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1;
  280. info->blksz = rdb.block_bytes;
  281. bcpl_strcpy(info->name, p->drive_name);
  282. disk_type = g->dos_type;
  283. info->type[0] = (disk_type & 0xFF000000)>>24;
  284. info->type[1] = (disk_type & 0x00FF0000)>>16;
  285. info->type[2] = (disk_type & 0x0000FF00)>>8;
  286. info->type[3] = '\\';
  287. info->type[4] = (disk_type & 0x000000FF) + '0';
  288. info->type[5] = 0;
  289. return 0;
  290. }
  291. void print_part_amiga (block_dev_desc_t *dev_desc)
  292. {
  293. struct rigid_disk_block *rdb;
  294. struct bootcode_block *boot;
  295. struct partition_block *p;
  296. u32 block;
  297. int i = 1;
  298. rdb = get_rdisk(dev_desc);
  299. if (!rdb)
  300. {
  301. PRINTF("print_part_amiga: no rdb found\n");
  302. return;
  303. }
  304. PRINTF("print_part_amiga: Scanning partition list\n");
  305. block = rdb->partition_list;
  306. PRINTF("print_part_amiga: partition list at 0x%x\n", block);
  307. printf("Summary: DiskBlockSize: %d\n"
  308. " Cylinders : %d\n"
  309. " Sectors/Track: %d\n"
  310. " Heads : %d\n\n",
  311. rdb->block_bytes, rdb->cylinders, rdb->sectors,
  312. rdb->heads);
  313. printf(" First Num. \n"
  314. "Nr. Part. Name Block Block Type Boot Priority\n");
  315. while (block != 0xFFFFFFFF)
  316. {
  317. ulong res;
  318. PRINTF("Trying to load block #0x%X\n", block);
  319. res = dev_desc->block_read(dev_desc->dev, block, 1,
  320. (ulong *)block_buffer);
  321. if (res == 1)
  322. {
  323. p = (struct partition_block *)block_buffer;
  324. if (p->id == AMIGA_ID_PART)
  325. {
  326. PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
  327. if (sum_block((struct block_header *)p) == 0)
  328. {
  329. printf("%-4d ", i); i++;
  330. print_part_info(p);
  331. block = p->next;
  332. }
  333. } else block = 0xFFFFFFFF;
  334. } else block = 0xFFFFFFFF;
  335. }
  336. boot = get_bootcode(dev_desc);
  337. if (boot)
  338. {
  339. printf("Disk is bootable\n");
  340. }
  341. }
  342. #endif