1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 1996-2000 Russell King.
5 * Scan ADFS partitions on hard disk drives. Unfortunately, there
6 * isn't a standard for partitioning drives on Acorn machines, so
7 * every single manufacturer of SCSI and IDE cards created their own
10 #include <linux/buffer_head.h>
11 #include <linux/adfs_fs.h>
16 * Partition types. (Oh for reusability)
18 #define PARTITION_RISCIX_MFM 1
19 #define PARTITION_RISCIX_SCSI 2
20 #define PARTITION_LINUX 9
22 #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
23 defined(CONFIG_ACORN_PARTITION_ADFS)
24 static struct adfs_discrecord
*
25 adfs_partition(struct parsed_partitions
*state
, char *name
, char *data
,
26 unsigned long first_sector
, int slot
)
28 struct adfs_discrecord
*dr
;
29 unsigned int nr_sects
;
31 if (adfs_checkbblk(data
))
34 dr
= (struct adfs_discrecord
*)(data
+ 0x1c0);
36 if (dr
->disc_size
== 0 && dr
->disc_size_high
== 0)
39 nr_sects
= (le32_to_cpu(dr
->disc_size_high
) << 23) |
40 (le32_to_cpu(dr
->disc_size
) >> 9);
43 strlcat(state
->pp_buf
, " [", PAGE_SIZE
);
44 strlcat(state
->pp_buf
, name
, PAGE_SIZE
);
45 strlcat(state
->pp_buf
, "]", PAGE_SIZE
);
47 put_partition(state
, slot
, first_sector
, nr_sects
);
52 #ifdef CONFIG_ACORN_PARTITION_RISCIX
61 struct riscix_record
{
63 #define RISCIX_MAGIC cpu_to_le32(0x4a657320)
65 struct riscix_part part
[8];
68 #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
69 defined(CONFIG_ACORN_PARTITION_ADFS)
70 static int riscix_partition(struct parsed_partitions
*state
,
71 unsigned long first_sect
, int slot
,
72 unsigned long nr_sects
)
75 struct riscix_record
*rr
;
77 rr
= read_part_sector(state
, first_sect
, §
);
81 strlcat(state
->pp_buf
, " [RISCiX]", PAGE_SIZE
);
84 if (rr
->magic
== RISCIX_MAGIC
) {
85 unsigned long size
= nr_sects
> 2 ? 2 : nr_sects
;
88 strlcat(state
->pp_buf
, " <", PAGE_SIZE
);
90 put_partition(state
, slot
++, first_sect
, size
);
91 for (part
= 0; part
< 8; part
++) {
92 if (rr
->part
[part
].one
&&
93 memcmp(rr
->part
[part
].name
, "All\0", 4)) {
94 put_partition(state
, slot
++,
95 le32_to_cpu(rr
->part
[part
].start
),
96 le32_to_cpu(rr
->part
[part
].length
));
97 strlcat(state
->pp_buf
, "(", PAGE_SIZE
);
98 strlcat(state
->pp_buf
, rr
->part
[part
].name
, PAGE_SIZE
);
99 strlcat(state
->pp_buf
, ")", PAGE_SIZE
);
103 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
105 put_partition(state
, slot
++, first_sect
, nr_sects
);
108 put_dev_sector(sect
);
114 #define LINUX_NATIVE_MAGIC 0xdeafa1de
115 #define LINUX_SWAP_MAGIC 0xdeafab1e
123 #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
124 defined(CONFIG_ACORN_PARTITION_ADFS)
125 static int linux_partition(struct parsed_partitions
*state
,
126 unsigned long first_sect
, int slot
,
127 unsigned long nr_sects
)
130 struct linux_part
*linuxp
;
131 unsigned long size
= nr_sects
> 2 ? 2 : nr_sects
;
133 strlcat(state
->pp_buf
, " [Linux]", PAGE_SIZE
);
135 put_partition(state
, slot
++, first_sect
, size
);
137 linuxp
= read_part_sector(state
, first_sect
, §
);
141 strlcat(state
->pp_buf
, " <", PAGE_SIZE
);
142 while (linuxp
->magic
== cpu_to_le32(LINUX_NATIVE_MAGIC
) ||
143 linuxp
->magic
== cpu_to_le32(LINUX_SWAP_MAGIC
)) {
144 if (slot
== state
->limit
)
146 put_partition(state
, slot
++, first_sect
+
147 le32_to_cpu(linuxp
->start_sect
),
148 le32_to_cpu(linuxp
->nr_sects
));
151 strlcat(state
->pp_buf
, " >", PAGE_SIZE
);
153 put_dev_sector(sect
);
158 #ifdef CONFIG_ACORN_PARTITION_CUMANA
159 int adfspart_check_CUMANA(struct parsed_partitions
*state
)
161 unsigned long first_sector
= 0;
162 unsigned int start_blk
= 0;
165 char *name
= "CUMANA/ADFS";
170 * Try Cumana style partitions - sector 6 contains ADFS boot block
171 * with pointer to next 'drive'.
173 * There are unknowns in this code - is the 'cylinder number' of the
174 * next partition relative to the start of this one - I'm assuming
177 * Also, which ID did Cumana use?
179 * This is totally unfinished, and will require more work to get it
180 * going. Hence it is totally untested.
183 struct adfs_discrecord
*dr
;
184 unsigned int nr_sects
;
186 data
= read_part_sector(state
, start_blk
* 2 + 6, §
);
190 if (slot
== state
->limit
)
193 dr
= adfs_partition(state
, name
, data
, first_sector
, slot
++);
199 nr_sects
= (data
[0x1fd] + (data
[0x1fe] << 8)) *
200 (dr
->heads
+ (dr
->lowsector
& 0x40 ? 1 : 0)) *
207 first_sector
+= nr_sects
;
208 start_blk
+= nr_sects
>> (BLOCK_SIZE_BITS
- 9);
209 nr_sects
= 0; /* hmm - should be partition size */
211 switch (data
[0x1fc] & 15) {
212 case 0: /* No partition / ADFS? */
215 #ifdef CONFIG_ACORN_PARTITION_RISCIX
216 case PARTITION_RISCIX_SCSI
:
217 /* RISCiX - we don't know how to find the next one. */
218 slot
= riscix_partition(state
, first_sector
, slot
,
223 case PARTITION_LINUX
:
224 slot
= linux_partition(state
, first_sector
, slot
,
228 put_dev_sector(sect
);
232 put_dev_sector(sect
);
233 return first
? 0 : 1;
237 #ifdef CONFIG_ACORN_PARTITION_ADFS
239 * Purpose: allocate ADFS partitions.
241 * Params : hd - pointer to gendisk structure to store partition info.
242 * dev - device number to access.
244 * Returns: -1 on error, 0 for no ADFS boot sector, 1 for ok.
246 * Alloc : hda = whole drive
247 * hda1 = ADFS partition on first drive.
248 * hda2 = non-ADFS partition.
250 int adfspart_check_ADFS(struct parsed_partitions
*state
)
252 unsigned long start_sect
, nr_sects
, sectscyl
, heads
;
255 struct adfs_discrecord
*dr
;
259 data
= read_part_sector(state
, 6, §
);
263 dr
= adfs_partition(state
, "ADFS", data
, 0, slot
++);
265 put_dev_sector(sect
);
269 heads
= dr
->heads
+ ((dr
->lowsector
>> 6) & 1);
270 sectscyl
= dr
->secspertrack
* heads
;
271 start_sect
= ((data
[0x1fe] << 8) + data
[0x1fd]) * sectscyl
;
272 id
= data
[0x1fc] & 15;
273 put_dev_sector(sect
);
276 * Work out start of non-adfs partition.
278 nr_sects
= get_capacity(state
->disk
) - start_sect
;
282 #ifdef CONFIG_ACORN_PARTITION_RISCIX
283 case PARTITION_RISCIX_SCSI
:
284 case PARTITION_RISCIX_MFM
:
285 riscix_partition(state
, start_sect
, slot
,
290 case PARTITION_LINUX
:
291 linux_partition(state
, start_sect
, slot
,
296 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
301 #ifdef CONFIG_ACORN_PARTITION_ICS
308 static int adfspart_check_ICSLinux(struct parsed_partitions
*state
,
312 unsigned char *data
= read_part_sector(state
, block
, §
);
316 if (memcmp(data
, "LinuxPart", 9) == 0)
318 put_dev_sector(sect
);
325 * Check for a valid ICS partition using the checksum.
327 static inline int valid_ics_sector(const unsigned char *data
)
332 for (i
= 0, sum
= 0x50617274; i
< 508; i
++)
335 sum
-= le32_to_cpu(*(__le32
*)(&data
[508]));
341 * Purpose: allocate ICS partitions.
342 * Params : hd - pointer to gendisk structure to store partition info.
343 * dev - device number to access.
344 * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
345 * Alloc : hda = whole drive
346 * hda1 = ADFS partition 0 on first drive.
347 * hda2 = ADFS partition 1 on first drive.
350 int adfspart_check_ICS(struct parsed_partitions
*state
)
352 const unsigned char *data
;
353 const struct ics_part
*p
;
358 * Try ICS style partitions - sector 0 contains partition info.
360 data
= read_part_sector(state
, 0, §
);
364 if (!valid_ics_sector(data
)) {
365 put_dev_sector(sect
);
369 strlcat(state
->pp_buf
, " [ICS]", PAGE_SIZE
);
371 for (slot
= 1, p
= (const struct ics_part
*)data
; p
->size
; p
++) {
372 u32 start
= le32_to_cpu(p
->start
);
373 s32 size
= le32_to_cpu(p
->size
); /* yes, it's signed. */
375 if (slot
== state
->limit
)
379 * Negative sizes tell the RISC OS ICS driver to ignore
380 * this partition - in effect it says that this does not
381 * contain an ADFS filesystem.
387 * Our own extension - We use the first sector
388 * of the partition to identify what type this
389 * partition is. We must not make this visible
392 if (size
> 1 && adfspart_check_ICSLinux(state
, start
)) {
399 put_partition(state
, slot
++, start
, size
);
402 put_dev_sector(sect
);
403 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
408 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
418 static inline int valid_ptec_sector(const unsigned char *data
)
420 unsigned char checksum
= 0x2a;
424 * If it looks like a PC/BIOS partition, then it
425 * probably isn't PowerTec.
427 if (data
[510] == 0x55 && data
[511] == 0xaa)
430 for (i
= 0; i
< 511; i
++)
433 return checksum
== data
[511];
437 * Purpose: allocate ICS partitions.
438 * Params : hd - pointer to gendisk structure to store partition info.
439 * dev - device number to access.
440 * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
441 * Alloc : hda = whole drive
442 * hda1 = ADFS partition 0 on first drive.
443 * hda2 = ADFS partition 1 on first drive.
446 int adfspart_check_POWERTEC(struct parsed_partitions
*state
)
449 const unsigned char *data
;
450 const struct ptec_part
*p
;
454 data
= read_part_sector(state
, 0, §
);
458 if (!valid_ptec_sector(data
)) {
459 put_dev_sector(sect
);
463 strlcat(state
->pp_buf
, " [POWERTEC]", PAGE_SIZE
);
465 for (i
= 0, p
= (const struct ptec_part
*)data
; i
< 12; i
++, p
++) {
466 u32 start
= le32_to_cpu(p
->start
);
467 u32 size
= le32_to_cpu(p
->size
);
470 put_partition(state
, slot
++, start
, size
);
473 put_dev_sector(sect
);
474 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
479 #ifdef CONFIG_ACORN_PARTITION_EESOX
490 * Guess who created this format?
492 static const char eesox_name
[] = {
493 'N', 'e', 'i', 'l', ' ',
494 'C', 'r', 'i', 't', 'c', 'h', 'e', 'l', 'l', ' ', ' '
498 * EESOX SCSI partition format.
500 * This is a goddamned awful partition format. We don't seem to store
501 * the size of the partition in this table, only the start addresses.
503 * There are two possibilities where the size comes from:
504 * 1. The individual ADFS boot block entries that are placed on the disk.
505 * 2. The start address of the next entry.
507 int adfspart_check_EESOX(struct parsed_partitions
*state
)
510 const unsigned char *data
;
511 unsigned char buffer
[256];
512 struct eesox_part
*p
;
516 data
= read_part_sector(state
, 7, §
);
521 * "Decrypt" the partition table. God knows why...
523 for (i
= 0; i
< 256; i
++)
524 buffer
[i
] = data
[i
] ^ eesox_name
[i
& 15];
526 put_dev_sector(sect
);
528 for (i
= 0, p
= (struct eesox_part
*)buffer
; i
< 8; i
++, p
++) {
531 if (memcmp(p
->magic
, "Eesox", 6))
534 next
= le32_to_cpu(p
->start
);
536 put_partition(state
, slot
++, start
, next
- start
);
543 size
= get_capacity(state
->disk
);
544 put_partition(state
, slot
++, start
, size
- start
);
545 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);