part_amiga.c 9.5 KB

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