1 // SPDX-License-Identifier: GPL-2.0+
4 * Hans-Joerg Frieden, Hyperion Entertainment
5 * Hans-JoergF@hyperion-entertainment.com
11 #include "part_amiga.h"
14 #ifdef CONFIG_HAVE_BLOCK_DEVICE
19 #define PRINTF(fmt, args...) printf(fmt ,##args)
21 #define PRINTF(fmt, args...)
31 static unsigned char block_buffer[DEFAULT_SECTOR_SIZE];
32 static struct rigid_disk_block rdb = {0};
33 static struct bootcode_block bootcode = {0};
36 * Copy a bcpl to a c string
38 static void bcpl_strcpy(char *to, char *from)
51 * Print a BCPL String. BCPL strings start with a byte with the length
52 * of the string, and don't contain a terminating nul character
54 static void bstr_print(char *string)
63 buffer[i++] = *string++;
68 printf("%-10s", buffer);
72 * Sum a block. The checksum of a block must end up at zero
73 * to be valid. The chk_sum field is selected so that adding
76 int sum_block(struct block_header *header)
78 s32 *block = (s32 *)header;
82 for (i = 0; i < header->summed_longs; i++)
89 * Print an AmigaOS disk type. Disk types are a four-byte identifier
90 * describing the file system. They are usually written as a three-letter
91 * word followed by a backslash and a version number. For example,
92 * DOS\0 would be the original file system. SFS\0 would be SmartFileSystem.
95 static void print_disk_type(u32 disk_type)
98 buffer[0] = (disk_type & 0xFF000000)>>24;
99 buffer[1] = (disk_type & 0x00FF0000)>>16;
100 buffer[2] = (disk_type & 0x0000FF00)>>8;
102 buffer[4] = (disk_type & 0x000000FF) + '0';
104 printf("%s", buffer);
108 * Print the info contained within the given partition block
110 static void print_part_info(struct partition_block *p)
112 struct amiga_part_geometry *g;
114 g = (struct amiga_part_geometry *)&(p->environment);
116 bstr_print(p->drive_name);
118 g->low_cyl * g->block_per_track * g->surfaces ,
119 (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1);
120 print_disk_type(g->dos_type);
121 printf("\t%5d\n", g->boot_priority);
125 * Search for the Rigid Disk Block. The rigid disk block is required
126 * to be within the first 16 blocks of a drive, needs to have
127 * the ID AMIGA_ID_RDISK ('RDSK') and needs to have a valid
128 * sum-to-zero checksum
130 struct rigid_disk_block *get_rdisk(struct blk_desc *dev_desc)
136 s = env_get("amiga_scanlimit");
138 limit = simple_strtoul(s, NULL, 10);
140 limit = AMIGA_BLOCK_LIMIT;
142 for (i=0; i<limit; i++)
144 ulong res = blk_dread(dev_desc, i, 1, (ulong *)block_buffer);
147 struct rigid_disk_block *trdb = (struct rigid_disk_block *)block_buffer;
148 if (trdb->id == AMIGA_ID_RDISK)
150 PRINTF("Rigid disk block suspect at %d, checking checksum\n",i);
151 if (sum_block((struct block_header *)block_buffer) == 0)
154 memcpy(&rdb, trdb, sizeof(struct rigid_disk_block));
155 return (struct rigid_disk_block *)&rdb;
160 PRINTF("Done scanning, no RDB found\n");
165 * Search for boot code
166 * Again, the first boot block must be located somewhere in the first 16 blocks, or rooted in the
170 struct bootcode_block *get_bootcode(struct blk_desc *dev_desc)
176 s = env_get("amiga_scanlimit");
178 limit = simple_strtoul(s, NULL, 10);
180 limit = AMIGA_BLOCK_LIMIT;
182 PRINTF("Scanning for BOOT from 0 to %d\n", limit);
184 for (i = 0; i < limit; i++)
186 ulong res = blk_dread(dev_desc, i, 1, (ulong *)block_buffer);
189 struct bootcode_block *boot = (struct bootcode_block *)block_buffer;
190 if (boot->id == AMIGA_ID_BOOT)
192 PRINTF("BOOT block at %d, checking checksum\n", i);
193 if (sum_block((struct block_header *)block_buffer) == 0)
195 PRINTF("Found valid bootcode block\n");
196 memcpy(&bootcode, boot, sizeof(struct bootcode_block));
203 PRINTF("No boot code found on disk\n");
208 * Test if the given partition has an Amiga partition table/Rigid
211 static int part_test_amiga(struct blk_desc *dev_desc)
213 struct rigid_disk_block *rdb;
214 struct bootcode_block *bootcode;
216 PRINTF("part_test_amiga: Testing for an Amiga RDB partition\n");
218 rdb = get_rdisk(dev_desc);
221 bootcode = get_bootcode(dev_desc);
223 PRINTF("part_test_amiga: bootable Amiga disk\n");
225 PRINTF("part_test_amiga: non-bootable Amiga disk\n");
231 PRINTF("part_test_amiga: no RDB found\n");
238 * Find partition number partnum on the given drive.
240 static struct partition_block *find_partition(struct blk_desc *dev_desc,
243 struct rigid_disk_block *rdb;
244 struct partition_block *p;
247 PRINTF("Trying to find partition block %d\n", partnum);
248 rdb = get_rdisk(dev_desc);
251 PRINTF("find_partition: no rdb found\n");
255 PRINTF("find_partition: Scanning partition list\n");
257 block = rdb->partition_list;
258 PRINTF("find_partition: partition list at 0x%x\n", block);
260 while (block != 0xFFFFFFFF)
262 ulong res = blk_dread(dev_desc, block, 1, (ulong *)block_buffer);
265 p = (struct partition_block *)block_buffer;
266 if (p->id == AMIGA_ID_PART)
268 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
269 if (sum_block((struct block_header *)p) == 0)
271 if (partnum == 0) break;
278 } else block = 0xFFFFFFFF;
279 } else block = 0xFFFFFFFF;
282 if (block == 0xFFFFFFFF)
284 PRINTF("PART block not found\n");
288 return (struct partition_block *)block_buffer;
292 * Get info about a partition
294 static int part_get_info_amiga(struct blk_desc *dev_desc, int part,
295 struct disk_partition *info)
297 struct partition_block *p = find_partition(dev_desc, part-1);
298 struct amiga_part_geometry *g;
303 g = (struct amiga_part_geometry *)&(p->environment);
304 info->start = g->low_cyl * g->block_per_track * g->surfaces;
305 info->size = (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1;
306 info->blksz = rdb.block_bytes;
307 bcpl_strcpy((char *)info->name, p->drive_name);
310 disk_type = g->dos_type;
312 info->type[0] = (disk_type & 0xFF000000)>>24;
313 info->type[1] = (disk_type & 0x00FF0000)>>16;
314 info->type[2] = (disk_type & 0x0000FF00)>>8;
315 info->type[3] = '\\';
316 info->type[4] = (disk_type & 0x000000FF) + '0';
322 static void part_print_amiga(struct blk_desc *dev_desc)
324 struct rigid_disk_block *rdb;
325 struct bootcode_block *boot;
326 struct partition_block *p;
330 rdb = get_rdisk(dev_desc);
333 PRINTF("part_print_amiga: no rdb found\n");
337 PRINTF("part_print_amiga: Scanning partition list\n");
339 block = rdb->partition_list;
340 PRINTF("part_print_amiga: partition list at 0x%x\n", block);
342 printf("Summary: DiskBlockSize: %d\n"
344 " Sectors/Track: %d\n"
346 rdb->block_bytes, rdb->cylinders, rdb->sectors,
349 printf(" First Num. \n"
350 "Nr. Part. Name Block Block Type Boot Priority\n");
352 while (block != 0xFFFFFFFF)
356 PRINTF("Trying to load block #0x%X\n", block);
358 res = blk_dread(dev_desc, block, 1, (ulong *)block_buffer);
361 p = (struct partition_block *)block_buffer;
362 if (p->id == AMIGA_ID_PART)
364 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
365 if (sum_block((struct block_header *)p) == 0)
367 printf("%-4d ", i); i++;
371 } else block = 0xFFFFFFFF;
372 } else block = 0xFFFFFFFF;
375 boot = get_bootcode(dev_desc);
378 printf("Disk is bootable\n");
382 U_BOOT_PART_TYPE(amiga) = {
384 .part_type = PART_TYPE_AMIGA,
385 .max_entries = AMIGA_ENTRY_NUMBERS,
386 .get_info = part_get_info_amiga,
387 .print = part_print_amiga,
388 .test = part_test_amiga,