part_amiga.c 8.8 KB

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