3 * Hans-Joerg Frieden, Hyperion Entertainment
4 * Hans-JoergF@hyperion-entertainment.com
6 * SPDX-License-Identifier: GPL-2.0+
11 #include "part_amiga.h"
13 #ifdef CONFIG_HAVE_BLOCK_DEVICE
18 #define PRINTF(fmt, args...) printf(fmt ,##args)
20 #define PRINTF(fmt, args...)
30 static unsigned char block_buffer[DEFAULT_SECTOR_SIZE];
31 static struct rigid_disk_block rdb = {0};
32 static struct bootcode_block bootcode = {0};
35 * Copy a bcpl to a c string
37 static void bcpl_strcpy(char *to, char *from)
50 * Print a BCPL String. BCPL strings start with a byte with the length
51 * of the string, and don't contain a terminating nul character
53 static void bstr_print(char *string)
62 buffer[i++] = *string++;
67 printf("%-10s", buffer);
71 * Sum a block. The checksum of a block must end up at zero
72 * to be valid. The chk_sum field is selected so that adding
75 int sum_block(struct block_header *header)
77 s32 *block = (s32 *)header;
81 for (i = 0; i < header->summed_longs; i++)
88 * Print an AmigaOS disk type. Disk types are a four-byte identifier
89 * describing the file system. They are usually written as a three-letter
90 * word followed by a backslash and a version number. For example,
91 * DOS\0 would be the original file system. SFS\0 would be SmartFileSystem.
94 static void print_disk_type(u32 disk_type)
97 buffer[0] = (disk_type & 0xFF000000)>>24;
98 buffer[1] = (disk_type & 0x00FF0000)>>16;
99 buffer[2] = (disk_type & 0x0000FF00)>>8;
101 buffer[4] = (disk_type & 0x000000FF) + '0';
103 printf("%s", buffer);
107 * Print the info contained within the given partition block
109 static void print_part_info(struct partition_block *p)
111 struct amiga_part_geometry *g;
113 g = (struct amiga_part_geometry *)&(p->environment);
115 bstr_print(p->drive_name);
117 g->low_cyl * g->block_per_track * g->surfaces ,
118 (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1);
119 print_disk_type(g->dos_type);
120 printf("\t%5d\n", g->boot_priority);
124 * Search for the Rigid Disk Block. The rigid disk block is required
125 * to be within the first 16 blocks of a drive, needs to have
126 * the ID AMIGA_ID_RDISK ('RDSK') and needs to have a valid
127 * sum-to-zero checksum
129 struct rigid_disk_block *get_rdisk(struct blk_desc *dev_desc)
135 s = env_get("amiga_scanlimit");
137 limit = simple_strtoul(s, NULL, 10);
139 limit = AMIGA_BLOCK_LIMIT;
141 for (i=0; i<limit; i++)
143 ulong res = blk_dread(dev_desc, i, 1, (ulong *)block_buffer);
146 struct rigid_disk_block *trdb = (struct rigid_disk_block *)block_buffer;
147 if (trdb->id == AMIGA_ID_RDISK)
149 PRINTF("Rigid disk block suspect at %d, checking checksum\n",i);
150 if (sum_block((struct block_header *)block_buffer) == 0)
153 memcpy(&rdb, trdb, sizeof(struct rigid_disk_block));
154 return (struct rigid_disk_block *)&rdb;
159 PRINTF("Done scanning, no RDB found\n");
164 * Search for boot code
165 * Again, the first boot block must be located somewhere in the first 16 blocks, or rooted in the
169 struct bootcode_block *get_bootcode(struct blk_desc *dev_desc)
175 s = env_get("amiga_scanlimit");
177 limit = simple_strtoul(s, NULL, 10);
179 limit = AMIGA_BLOCK_LIMIT;
181 PRINTF("Scanning for BOOT from 0 to %d\n", limit);
183 for (i = 0; i < limit; i++)
185 ulong res = blk_dread(dev_desc, i, 1, (ulong *)block_buffer);
188 struct bootcode_block *boot = (struct bootcode_block *)block_buffer;
189 if (boot->id == AMIGA_ID_BOOT)
191 PRINTF("BOOT block at %d, checking checksum\n", i);
192 if (sum_block((struct block_header *)block_buffer) == 0)
194 PRINTF("Found valid bootcode block\n");
195 memcpy(&bootcode, boot, sizeof(struct bootcode_block));
202 PRINTF("No boot code found on disk\n");
207 * Test if the given partition has an Amiga partition table/Rigid
210 static int part_test_amiga(struct blk_desc *dev_desc)
212 struct rigid_disk_block *rdb;
213 struct bootcode_block *bootcode;
215 PRINTF("part_test_amiga: Testing for an Amiga RDB partition\n");
217 rdb = get_rdisk(dev_desc);
220 bootcode = get_bootcode(dev_desc);
222 PRINTF("part_test_amiga: bootable Amiga disk\n");
224 PRINTF("part_test_amiga: non-bootable Amiga disk\n");
230 PRINTF("part_test_amiga: no RDB found\n");
237 * Find partition number partnum on the given drive.
239 static struct partition_block *find_partition(struct blk_desc *dev_desc,
242 struct rigid_disk_block *rdb;
243 struct partition_block *p;
246 PRINTF("Trying to find partition block %d\n", partnum);
247 rdb = get_rdisk(dev_desc);
250 PRINTF("find_partition: no rdb found\n");
254 PRINTF("find_partition: Scanning partition list\n");
256 block = rdb->partition_list;
257 PRINTF("find_partition: partition list at 0x%x\n", block);
259 while (block != 0xFFFFFFFF)
261 ulong res = blk_dread(dev_desc, block, 1, (ulong *)block_buffer);
264 p = (struct partition_block *)block_buffer;
265 if (p->id == AMIGA_ID_PART)
267 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
268 if (sum_block((struct block_header *)p) == 0)
270 if (partnum == 0) break;
277 } else block = 0xFFFFFFFF;
278 } else block = 0xFFFFFFFF;
281 if (block == 0xFFFFFFFF)
283 PRINTF("PART block not found\n");
287 return (struct partition_block *)block_buffer;
291 * Get info about a partition
293 static int part_get_info_amiga(struct blk_desc *dev_desc, int part,
294 disk_partition_t *info)
296 struct partition_block *p = find_partition(dev_desc, part-1);
297 struct amiga_part_geometry *g;
302 g = (struct amiga_part_geometry *)&(p->environment);
303 info->start = g->low_cyl * g->block_per_track * g->surfaces;
304 info->size = (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1;
305 info->blksz = rdb.block_bytes;
306 bcpl_strcpy((char *)info->name, p->drive_name);
309 disk_type = g->dos_type;
311 info->type[0] = (disk_type & 0xFF000000)>>24;
312 info->type[1] = (disk_type & 0x00FF0000)>>16;
313 info->type[2] = (disk_type & 0x0000FF00)>>8;
314 info->type[3] = '\\';
315 info->type[4] = (disk_type & 0x000000FF) + '0';
321 static void part_print_amiga(struct blk_desc *dev_desc)
323 struct rigid_disk_block *rdb;
324 struct bootcode_block *boot;
325 struct partition_block *p;
329 rdb = get_rdisk(dev_desc);
332 PRINTF("part_print_amiga: no rdb found\n");
336 PRINTF("part_print_amiga: Scanning partition list\n");
338 block = rdb->partition_list;
339 PRINTF("part_print_amiga: partition list at 0x%x\n", block);
341 printf("Summary: DiskBlockSize: %d\n"
343 " Sectors/Track: %d\n"
345 rdb->block_bytes, rdb->cylinders, rdb->sectors,
348 printf(" First Num. \n"
349 "Nr. Part. Name Block Block Type Boot Priority\n");
351 while (block != 0xFFFFFFFF)
355 PRINTF("Trying to load block #0x%X\n", block);
357 res = blk_dread(dev_desc, block, 1, (ulong *)block_buffer);
360 p = (struct partition_block *)block_buffer;
361 if (p->id == AMIGA_ID_PART)
363 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
364 if (sum_block((struct block_header *)p) == 0)
366 printf("%-4d ", i); i++;
370 } else block = 0xFFFFFFFF;
371 } else block = 0xFFFFFFFF;
374 boot = get_bootcode(dev_desc);
377 printf("Disk is bootable\n");
381 U_BOOT_PART_TYPE(amiga) = {
383 .part_type = PART_TYPE_AMIGA,
384 .max_entries = AMIGA_ENTRY_NUMBERS,
385 .get_info = part_get_info_amiga,
386 .print = part_print_amiga,
387 .test = part_test_amiga,