part_amiga.c 9.4 KB

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