part_amiga.c 8.8 KB

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