part_amiga.c 9.5 KB

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