part_amiga.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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, 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(block_dev_desc_t *dev_desc)
  148. {
  149. int i;
  150. int limit;
  151. char *s;
  152. s = getenv("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 = dev_desc->block_read(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. int test_part_amiga(block_dev_desc_t *dev_desc)
  184. {
  185. struct rigid_disk_block *rdb;
  186. struct bootcode_block *bootcode;
  187. PRINTF("test_part_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("test_part_amiga: bootable Amiga disk\n");
  194. else
  195. PRINTF("test_part_amiga: non-bootable Amiga disk\n");
  196. return 0;
  197. }
  198. else
  199. {
  200. PRINTF("test_part_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(block_dev_desc_t *dev_desc, 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 = dev_desc->block_read(dev_desc, block, 1,
  225. (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. int get_partition_info_amiga (block_dev_desc_t *dev_desc, int part, 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(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. void print_part_amiga (block_dev_desc_t *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("print_part_amiga: no rdb found\n");
  285. return;
  286. }
  287. PRINTF("print_part_amiga: Scanning partition list\n");
  288. block = rdb->partition_list;
  289. PRINTF("print_part_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 = dev_desc->block_read(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. #endif