part_amiga.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * (C) Copyright 2001
  3. * Hans-Joerg Frieden, Hyperion Entertainment
  4. * Hans-JoergF@hyperion-entertainment.com
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <ide.h>
  11. #include "part_amiga.h"
  12. #ifdef 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(block_dev_desc_t *dev_desc)
  112. {
  113. int i;
  114. int limit;
  115. char *s;
  116. s = getenv("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 = dev_desc->block_read(dev_desc->dev, i, 1,
  124. (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(block_dev_desc_t *dev_desc)
  149. {
  150. int i;
  151. int limit;
  152. char *s;
  153. s = getenv("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 = dev_desc->block_read(dev_desc->dev, 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. int test_part_amiga(block_dev_desc_t *dev_desc)
  185. {
  186. struct rigid_disk_block *rdb;
  187. struct bootcode_block *bootcode;
  188. PRINTF("test_part_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("test_part_amiga: bootable Amiga disk\n");
  195. else
  196. PRINTF("test_part_amiga: non-bootable Amiga disk\n");
  197. return 0;
  198. }
  199. else
  200. {
  201. PRINTF("test_part_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(block_dev_desc_t *dev_desc, 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 = dev_desc->block_read(dev_desc->dev, block, 1,
  226. (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. int get_partition_info_amiga (block_dev_desc_t *dev_desc, int part, 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(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. void print_part_amiga (block_dev_desc_t *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("print_part_amiga: no rdb found\n");
  286. return;
  287. }
  288. PRINTF("print_part_amiga: Scanning partition list\n");
  289. block = rdb->partition_list;
  290. PRINTF("print_part_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 = dev_desc->block_read(dev_desc->dev, block, 1,
  304. (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. #endif