part_amiga.c 8.8 KB

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