blob: 22bf99f1b88c240d1885013c22b59e95e9bcbf70 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk452cfd62002-11-19 11:04:11 +00002/*
3 * (C) Copyright 2001
wdenk57b2d802003-06-27 21:31:46 +00004 * Hans-Joerg Frieden, Hyperion Entertainment
wdenk452cfd62002-11-19 11:04:11 +00005 * Hans-JoergF@hyperion-entertainment.com
wdenk452cfd62002-11-19 11:04:11 +00006 */
wdenk452cfd62002-11-19 11:04:11 +00007#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06008#include <env.h>
wdenk452cfd62002-11-19 11:04:11 +00009#include "part_amiga.h"
Simon Glass655306c2020-05-10 11:39:58 -060010#include <part.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <vsprintf.h>
wdenk452cfd62002-11-19 11:04:11 +000012
wdenk452cfd62002-11-19 11:04:11 +000013#undef AMIGA_DEBUG
14
15#ifdef AMIGA_DEBUG
16#define PRINTF(fmt, args...) printf(fmt ,##args)
17#else
18#define PRINTF(fmt, args...)
19#endif
20
21struct block_header
22{
23 u32 id;
24 u32 summed_longs;
25 s32 chk_sum;
26};
27
28static unsigned char block_buffer[DEFAULT_SECTOR_SIZE];
29static struct rigid_disk_block rdb = {0};
30static struct bootcode_block bootcode = {0};
31
32/*
33 * Copy a bcpl to a c string
34 */
35static void bcpl_strcpy(char *to, char *from)
36{
37 int len = *from++;
38
39 while (len)
40 {
41 *to++ = *from++;
42 len--;
43 }
44 *to = 0;
45}
46
47/*
48 * Print a BCPL String. BCPL strings start with a byte with the length
49 * of the string, and don't contain a terminating nul character
50 */
51static void bstr_print(char *string)
52{
53 int len = *string++;
54 char buffer[256];
55 int i;
wdenk57b2d802003-06-27 21:31:46 +000056
wdenk452cfd62002-11-19 11:04:11 +000057 i = 0;
58 while (len)
59 {
60 buffer[i++] = *string++;
61 len--;
62 }
63
64 buffer[i] = 0;
65 printf("%-10s", buffer);
66}
67
68/*
69 * Sum a block. The checksum of a block must end up at zero
70 * to be valid. The chk_sum field is selected so that adding
71 * it yields zero.
72 */
73int sum_block(struct block_header *header)
74{
75 s32 *block = (s32 *)header;
76 u32 i;
77 s32 sum = 0;
78
79 for (i = 0; i < header->summed_longs; i++)
80 sum += *block++;
wdenk57b2d802003-06-27 21:31:46 +000081
wdenk452cfd62002-11-19 11:04:11 +000082 return (sum != 0);
83}
84
85/*
86 * Print an AmigaOS disk type. Disk types are a four-byte identifier
87 * describing the file system. They are usually written as a three-letter
88 * word followed by a backslash and a version number. For example,
89 * DOS\0 would be the original file system. SFS\0 would be SmartFileSystem.
90 * DOS\1 is FFS.
91 */
92static void print_disk_type(u32 disk_type)
93{
94 char buffer[6];
95 buffer[0] = (disk_type & 0xFF000000)>>24;
96 buffer[1] = (disk_type & 0x00FF0000)>>16;
97 buffer[2] = (disk_type & 0x0000FF00)>>8;
98 buffer[3] = '\\';
99 buffer[4] = (disk_type & 0x000000FF) + '0';
100 buffer[5] = 0;
101 printf("%s", buffer);
102}
103
104/*
105 * Print the info contained within the given partition block
106 */
107static void print_part_info(struct partition_block *p)
108{
109 struct amiga_part_geometry *g;
wdenk57b2d802003-06-27 21:31:46 +0000110
wdenk452cfd62002-11-19 11:04:11 +0000111 g = (struct amiga_part_geometry *)&(p->environment);
112
113 bstr_print(p->drive_name);
wdenk57b2d802003-06-27 21:31:46 +0000114 printf("%6d\t%6d\t",
115 g->low_cyl * g->block_per_track * g->surfaces ,
wdenk452cfd62002-11-19 11:04:11 +0000116 (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1);
117 print_disk_type(g->dos_type);
118 printf("\t%5d\n", g->boot_priority);
119}
120
121/*
122 * Search for the Rigid Disk Block. The rigid disk block is required
123 * to be within the first 16 blocks of a drive, needs to have
124 * the ID AMIGA_ID_RDISK ('RDSK') and needs to have a valid
125 * sum-to-zero checksum
126 */
Simon Glass17d797b2023-08-24 13:55:25 -0600127struct rigid_disk_block *get_rdisk(struct blk_desc *desc)
wdenk452cfd62002-11-19 11:04:11 +0000128{
129 int i;
130 int limit;
131 char *s;
132
Simon Glass64b723f2017-08-03 12:22:12 -0600133 s = env_get("amiga_scanlimit");
wdenk452cfd62002-11-19 11:04:11 +0000134 if (s)
Simon Glassff9b9032021-07-24 09:03:30 -0600135 limit = dectoul(s, NULL);
wdenk452cfd62002-11-19 11:04:11 +0000136 else
137 limit = AMIGA_BLOCK_LIMIT;
138
139 for (i=0; i<limit; i++)
140 {
Simon Glass17d797b2023-08-24 13:55:25 -0600141 ulong res = blk_dread(desc, i, 1, (ulong *)block_buffer);
wdenk452cfd62002-11-19 11:04:11 +0000142 if (res == 1)
143 {
144 struct rigid_disk_block *trdb = (struct rigid_disk_block *)block_buffer;
145 if (trdb->id == AMIGA_ID_RDISK)
146 {
147 PRINTF("Rigid disk block suspect at %d, checking checksum\n",i);
148 if (sum_block((struct block_header *)block_buffer) == 0)
149 {
150 PRINTF("FOUND\n");
151 memcpy(&rdb, trdb, sizeof(struct rigid_disk_block));
152 return (struct rigid_disk_block *)&rdb;
153 }
154 }
155 }
156 }
157 PRINTF("Done scanning, no RDB found\n");
158 return NULL;
159}
160
wdenk57b2d802003-06-27 21:31:46 +0000161/*
wdenk452cfd62002-11-19 11:04:11 +0000162 * Search for boot code
163 * Again, the first boot block must be located somewhere in the first 16 blocks, or rooted in the
164 * Ridgid disk block
165 */
166
Simon Glass17d797b2023-08-24 13:55:25 -0600167struct bootcode_block *get_bootcode(struct blk_desc *desc)
wdenk452cfd62002-11-19 11:04:11 +0000168{
169 int i;
170 int limit;
171 char *s;
172
Simon Glass64b723f2017-08-03 12:22:12 -0600173 s = env_get("amiga_scanlimit");
wdenk452cfd62002-11-19 11:04:11 +0000174 if (s)
Simon Glassff9b9032021-07-24 09:03:30 -0600175 limit = dectoul(s, NULL);
wdenk452cfd62002-11-19 11:04:11 +0000176 else
177 limit = AMIGA_BLOCK_LIMIT;
178
179 PRINTF("Scanning for BOOT from 0 to %d\n", limit);
180
181 for (i = 0; i < limit; i++)
182 {
Simon Glass17d797b2023-08-24 13:55:25 -0600183 ulong res = blk_dread(desc, i, 1, (ulong *)block_buffer);
wdenk452cfd62002-11-19 11:04:11 +0000184 if (res == 1)
185 {
186 struct bootcode_block *boot = (struct bootcode_block *)block_buffer;
187 if (boot->id == AMIGA_ID_BOOT)
188 {
189 PRINTF("BOOT block at %d, checking checksum\n", i);
190 if (sum_block((struct block_header *)block_buffer) == 0)
191 {
192 PRINTF("Found valid bootcode block\n");
193 memcpy(&bootcode, boot, sizeof(struct bootcode_block));
194 return &bootcode;
195 }
196 }
197 }
198 }
199
200 PRINTF("No boot code found on disk\n");
201 return 0;
202}
203
wdenk57b2d802003-06-27 21:31:46 +0000204/*
wdenk452cfd62002-11-19 11:04:11 +0000205 * Test if the given partition has an Amiga partition table/Rigid
206 * Disk block
207 */
Simon Glass17d797b2023-08-24 13:55:25 -0600208static int part_test_amiga(struct blk_desc *desc)
wdenk452cfd62002-11-19 11:04:11 +0000209{
210 struct rigid_disk_block *rdb;
211 struct bootcode_block *bootcode;
212
Simon Glass0f1c9522016-02-29 15:26:04 -0700213 PRINTF("part_test_amiga: Testing for an Amiga RDB partition\n");
wdenk57b2d802003-06-27 21:31:46 +0000214
Simon Glass17d797b2023-08-24 13:55:25 -0600215 rdb = get_rdisk(desc);
wdenk452cfd62002-11-19 11:04:11 +0000216 if (rdb)
217 {
Simon Glass17d797b2023-08-24 13:55:25 -0600218 bootcode = get_bootcode(desc);
wdenk452cfd62002-11-19 11:04:11 +0000219 if (bootcode)
Simon Glass0f1c9522016-02-29 15:26:04 -0700220 PRINTF("part_test_amiga: bootable Amiga disk\n");
wdenk452cfd62002-11-19 11:04:11 +0000221 else
Simon Glass0f1c9522016-02-29 15:26:04 -0700222 PRINTF("part_test_amiga: non-bootable Amiga disk\n");
wdenk452cfd62002-11-19 11:04:11 +0000223
224 return 0;
225 }
wdenk57b2d802003-06-27 21:31:46 +0000226 else
wdenk452cfd62002-11-19 11:04:11 +0000227 {
Simon Glass0f1c9522016-02-29 15:26:04 -0700228 PRINTF("part_test_amiga: no RDB found\n");
wdenk452cfd62002-11-19 11:04:11 +0000229 return -1;
230 }
231
232}
233
wdenk57b2d802003-06-27 21:31:46 +0000234/*
wdenk452cfd62002-11-19 11:04:11 +0000235 * Find partition number partnum on the given drive.
236 */
Simon Glass17d797b2023-08-24 13:55:25 -0600237static struct partition_block *find_partition(struct blk_desc *desc,
Simon Glasse3394752016-02-29 15:25:34 -0700238 int partnum)
wdenk452cfd62002-11-19 11:04:11 +0000239{
240 struct rigid_disk_block *rdb;
241 struct partition_block *p;
242 u32 block;
243
244 PRINTF("Trying to find partition block %d\n", partnum);
Simon Glass17d797b2023-08-24 13:55:25 -0600245 rdb = get_rdisk(desc);
wdenk57b2d802003-06-27 21:31:46 +0000246 if (!rdb)
wdenk452cfd62002-11-19 11:04:11 +0000247 {
248 PRINTF("find_partition: no rdb found\n");
249 return NULL;
250 }
wdenk57b2d802003-06-27 21:31:46 +0000251
wdenk452cfd62002-11-19 11:04:11 +0000252 PRINTF("find_partition: Scanning partition list\n");
253
254 block = rdb->partition_list;
255 PRINTF("find_partition: partition list at 0x%x\n", block);
256
257 while (block != 0xFFFFFFFF)
258 {
Simon Glass17d797b2023-08-24 13:55:25 -0600259 ulong res = blk_dread(desc, block, 1, (ulong *)block_buffer);
wdenk452cfd62002-11-19 11:04:11 +0000260 if (res == 1)
261 {
262 p = (struct partition_block *)block_buffer;
263 if (p->id == AMIGA_ID_PART)
264 {
265 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
266 if (sum_block((struct block_header *)p) == 0)
267 {
268 if (partnum == 0) break;
wdenk57b2d802003-06-27 21:31:46 +0000269 else
wdenk452cfd62002-11-19 11:04:11 +0000270 {
271 partnum--;
272 block = p->next;
273 }
274 }
275 } else block = 0xFFFFFFFF;
276 } else block = 0xFFFFFFFF;
277 }
278
wdenk57b2d802003-06-27 21:31:46 +0000279 if (block == 0xFFFFFFFF)
wdenk452cfd62002-11-19 11:04:11 +0000280 {
281 PRINTF("PART block not found\n");
282 return NULL;
283 }
284
285 return (struct partition_block *)block_buffer;
286}
287
wdenk57b2d802003-06-27 21:31:46 +0000288/*
wdenk452cfd62002-11-19 11:04:11 +0000289 * Get info about a partition
290 */
Simon Glass17d797b2023-08-24 13:55:25 -0600291static int part_get_info_amiga(struct blk_desc *desc, int part,
292 struct disk_partition *info)
wdenk452cfd62002-11-19 11:04:11 +0000293{
Simon Glass17d797b2023-08-24 13:55:25 -0600294 struct partition_block *p = find_partition(desc, part - 1);
wdenk452cfd62002-11-19 11:04:11 +0000295 struct amiga_part_geometry *g;
296 u32 disk_type;
297
298 if (!p) return -1;
299
300 g = (struct amiga_part_geometry *)&(p->environment);
301 info->start = g->low_cyl * g->block_per_track * g->surfaces;
302 info->size = (g->high_cyl - g->low_cyl + 1) * g->block_per_track * g->surfaces - 1;
303 info->blksz = rdb.block_bytes;
Simon Glass250cb2b2016-02-29 15:25:45 -0700304 bcpl_strcpy((char *)info->name, p->drive_name);
wdenk57b2d802003-06-27 21:31:46 +0000305
wdenk452cfd62002-11-19 11:04:11 +0000306 disk_type = g->dos_type;
307
308 info->type[0] = (disk_type & 0xFF000000)>>24;
309 info->type[1] = (disk_type & 0x00FF0000)>>16;
310 info->type[2] = (disk_type & 0x0000FF00)>>8;
311 info->type[3] = '\\';
312 info->type[4] = (disk_type & 0x000000FF) + '0';
313 info->type[5] = 0;
wdenk57b2d802003-06-27 21:31:46 +0000314
wdenk452cfd62002-11-19 11:04:11 +0000315 return 0;
316}
317
Simon Glass17d797b2023-08-24 13:55:25 -0600318static void part_print_amiga(struct blk_desc *desc)
wdenk57b2d802003-06-27 21:31:46 +0000319{
wdenk452cfd62002-11-19 11:04:11 +0000320 struct rigid_disk_block *rdb;
321 struct bootcode_block *boot;
322 struct partition_block *p;
323 u32 block;
324 int i = 1;
325
Simon Glass17d797b2023-08-24 13:55:25 -0600326 rdb = get_rdisk(desc);
wdenk57b2d802003-06-27 21:31:46 +0000327 if (!rdb)
wdenk452cfd62002-11-19 11:04:11 +0000328 {
Simon Glass0f1c9522016-02-29 15:26:04 -0700329 PRINTF("part_print_amiga: no rdb found\n");
wdenk452cfd62002-11-19 11:04:11 +0000330 return;
331 }
wdenk57b2d802003-06-27 21:31:46 +0000332
Simon Glass0f1c9522016-02-29 15:26:04 -0700333 PRINTF("part_print_amiga: Scanning partition list\n");
wdenk452cfd62002-11-19 11:04:11 +0000334
335 block = rdb->partition_list;
Simon Glass0f1c9522016-02-29 15:26:04 -0700336 PRINTF("part_print_amiga: partition list at 0x%x\n", block);
wdenk452cfd62002-11-19 11:04:11 +0000337
338 printf("Summary: DiskBlockSize: %d\n"
339 " Cylinders : %d\n"
340 " Sectors/Track: %d\n"
341 " Heads : %d\n\n",
342 rdb->block_bytes, rdb->cylinders, rdb->sectors,
343 rdb->heads);
344
345 printf(" First Num. \n"
346 "Nr. Part. Name Block Block Type Boot Priority\n");
347
348 while (block != 0xFFFFFFFF)
349 {
350 ulong res;
351
352 PRINTF("Trying to load block #0x%X\n", block);
wdenk57b2d802003-06-27 21:31:46 +0000353
Simon Glass17d797b2023-08-24 13:55:25 -0600354 res = blk_dread(desc, block, 1, (ulong *)block_buffer);
wdenk452cfd62002-11-19 11:04:11 +0000355 if (res == 1)
356 {
357 p = (struct partition_block *)block_buffer;
358 if (p->id == AMIGA_ID_PART)
359 {
360 PRINTF("PART block suspect at 0x%x, checking checksum\n",block);
361 if (sum_block((struct block_header *)p) == 0)
362 {
363 printf("%-4d ", i); i++;
364 print_part_info(p);
365 block = p->next;
366 }
367 } else block = 0xFFFFFFFF;
368 } else block = 0xFFFFFFFF;
369 }
370
Simon Glass17d797b2023-08-24 13:55:25 -0600371 boot = get_bootcode(desc);
wdenk452cfd62002-11-19 11:04:11 +0000372 if (boot)
373 {
374 printf("Disk is bootable\n");
375 }
376}
377
Simon Glass83ce5632016-02-29 15:25:47 -0700378U_BOOT_PART_TYPE(amiga) = {
379 .name = "AMIGA",
380 .part_type = PART_TYPE_AMIGA,
Petr Kulhavy712257e2016-09-09 10:27:15 +0200381 .max_entries = AMIGA_ENTRY_NUMBERS,
Simon Glassb89a8442016-02-29 15:25:48 -0700382 .get_info = part_get_info_amiga,
Simon Glass0f1c9522016-02-29 15:26:04 -0700383 .print = part_print_amiga,
384 .test = part_test_amiga,
Simon Glass83ce5632016-02-29 15:25:47 -0700385};