| 1 |
/* |
| 2 |
* atari partitions parsing code |
| 3 |
* |
| 4 |
* Copyright (C) 2018 Vaclav Dolezal <[email protected]> |
| 5 |
* |
| 6 |
* This file may be redistributed under the terms of the |
| 7 |
* GNU Lesser General Public License. |
| 8 |
* |
| 9 |
* Based on Linux kernel implementation and atari-fdisk |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <stdint.h> |
| 16 |
|
| 17 |
#include "partitions.h" |
| 18 |
|
| 19 |
struct atari_part_def { |
| 20 |
/* |
| 21 |
* flags: |
| 22 |
* 0 (LSB): active |
| 23 |
* 1-6: (reserved) |
| 24 |
* 7 (MSB): bootable |
| 25 |
*/ |
| 26 |
unsigned char flags; |
| 27 |
char id[3]; |
| 28 |
uint32_t start; |
| 29 |
uint32_t size; |
| 30 |
} __attribute__((packed)); |
| 31 |
|
| 32 |
struct atari_rootsector { |
| 33 |
char unused0[0x156]; /* boot code */ |
| 34 |
struct atari_part_def icd_part[8]; /* ICD partition entries */ |
| 35 |
char unused1[0xc]; |
| 36 |
uint32_t hd_size; |
| 37 |
struct atari_part_def part[4]; /* primary partition entries */ |
| 38 |
uint32_t bsl_start; /* bad sector list start */ |
| 39 |
uint32_t bsl_len; /* bad sector list length */ |
| 40 |
uint16_t checksum; |
| 41 |
} __attribute__((packed)); |
| 42 |
|
| 43 |
|
| 44 |
/* |
| 45 |
* Generated using linux kernel ctype.{c,h} |
| 46 |
* |
| 47 |
* Since kernel uses isalnum() to detect whether it is Atari PT, we need same |
| 48 |
* definition of alnum character to be consistent with kernel. |
| 49 |
*/ |
| 50 |
static const unsigned char _linux_isalnum[] = { |
| 51 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 52 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 53 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 54 |
1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, |
| 55 |
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 56 |
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, |
| 57 |
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 58 |
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, |
| 59 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 60 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 61 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 62 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 63 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 64 |
1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, |
| 65 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 66 |
1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1 |
| 67 |
}; |
| 68 |
|
| 69 |
static int linux_isalnum(unsigned char c) |
| 70 |
{ |
| 71 |
return _linux_isalnum[c]; |
| 72 |
} |
| 73 |
|
| 74 |
#define isalnum linux_isalnum |
| 75 |
|
| 76 |
#define IS_ACTIVE(partdef) ((partdef).flags & 1) |
| 77 |
|
| 78 |
static int is_valid_dimension(uint32_t start, uint32_t size, uint32_t maxoff) |
| 79 |
{ |
| 80 |
uint64_t end = start + size; |
| 81 |
|
| 82 |
return end >= start |
| 83 |
&& 0 < start && start <= maxoff |
| 84 |
&& 0 < size && size <= maxoff |
| 85 |
&& 0 < end && end <= maxoff; |
| 86 |
} |
| 87 |
|
| 88 |
static int is_valid_partition(struct atari_part_def *part, uint32_t maxoff) |
| 89 |
{ |
| 90 |
uint32_t start = be32_to_cpu(part->start), |
| 91 |
size = be32_to_cpu(part->size); |
| 92 |
|
| 93 |
return (part->flags & 1) |
| 94 |
&& isalnum(part->id[0]) |
| 95 |
&& isalnum(part->id[1]) |
| 96 |
&& isalnum(part->id[2]) |
| 97 |
&& is_valid_dimension(start, size, maxoff); |
| 98 |
} |
| 99 |
|
| 100 |
static int is_id_common(char *id) |
| 101 |
{ |
| 102 |
const char *ids[] = {"GEM", "BGM", "LNX", "SWP", "RAW", }; |
| 103 |
unsigned i; |
| 104 |
|
| 105 |
for (i = 0; i < ARRAY_SIZE(ids); i++) { |
| 106 |
if (!memcmp(ids[i], id, 3)) |
| 107 |
return 1; |
| 108 |
} |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static int parse_partition(blkid_partlist ls, blkid_parttable tab, |
| 113 |
struct atari_part_def *part, uint32_t offset) |
| 114 |
{ |
| 115 |
blkid_partition par; |
| 116 |
uint32_t start; |
| 117 |
uint32_t size; |
| 118 |
|
| 119 |
start = be32_to_cpu(part->start) + offset; |
| 120 |
size = be32_to_cpu(part->size); |
| 121 |
|
| 122 |
if (blkid_partlist_get_partition_by_start(ls, start)) { |
| 123 |
/* Don't increment partno for extended parts */ |
| 124 |
if (!offset) |
| 125 |
blkid_partlist_increment_partno(ls); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
par = blkid_partlist_add_partition(ls, tab, start, size); |
| 130 |
if (!par) |
| 131 |
return -ENOMEM; |
| 132 |
|
| 133 |
blkid_partition_set_type_string(par, (unsigned char *) part->id, |
| 134 |
sizeof(part->id)); |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
* \return 1: OK, 0: bad format or -errno |
| 140 |
*/ |
| 141 |
static int parse_extended(blkid_probe pr, blkid_partlist ls, |
| 142 |
blkid_parttable tab, struct atari_part_def *part) |
| 143 |
{ |
| 144 |
uint32_t x0start, xstart; |
| 145 |
unsigned ct = 0, i = 0; |
| 146 |
int rc; |
| 147 |
|
| 148 |
x0start = xstart = be32_to_cpu(part->start); |
| 149 |
while (1) { |
| 150 |
struct atari_rootsector *xrs; |
| 151 |
|
| 152 |
if (++ct > 100) |
| 153 |
break; |
| 154 |
|
| 155 |
xrs = (struct atari_rootsector *) blkid_probe_get_sector(pr, xstart); |
| 156 |
if (!xrs) { |
| 157 |
if (errno) |
| 158 |
return -errno; |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
/* |
| 163 |
* There must be data partition followed by reference to next |
| 164 |
* XGM or inactive entry. |
| 165 |
*/ |
| 166 |
for (i=0; ; i++) { |
| 167 |
if (i >= ARRAY_SIZE(xrs->part) - 1) |
| 168 |
return 0; |
| 169 |
if (IS_ACTIVE(xrs->part[i])) |
| 170 |
break; |
| 171 |
} |
| 172 |
|
| 173 |
if (!memcmp(xrs->part[i].id, "XGM", 3)) |
| 174 |
return 0; |
| 175 |
|
| 176 |
rc = parse_partition(ls, tab, &xrs->part[i], xstart); |
| 177 |
if (rc <= 0) |
| 178 |
return rc; |
| 179 |
|
| 180 |
if (!IS_ACTIVE(xrs->part[i+1])) |
| 181 |
break; |
| 182 |
|
| 183 |
if (memcmp(xrs->part[i+1].id, "XGM", 3) != 0) |
| 184 |
return 0; |
| 185 |
|
| 186 |
xstart = x0start + be32_to_cpu(xrs->part[i+1].start); |
| 187 |
} |
| 188 |
|
| 189 |
return 1; |
| 190 |
} |
| 191 |
|
| 192 |
static int probe_atari_pt(blkid_probe pr, |
| 193 |
const struct blkid_idmag *mag __attribute__((__unused__))) |
| 194 |
{ |
| 195 |
struct atari_rootsector *rs; |
| 196 |
|
| 197 |
blkid_parttable tab = NULL; |
| 198 |
blkid_partlist ls; |
| 199 |
|
| 200 |
unsigned i; |
| 201 |
int has_xgm = 0; |
| 202 |
int rc = 0; |
| 203 |
uint32_t rssize; /* size in sectors from root sector */ |
| 204 |
uint64_t size; /* size in sectors from system */ |
| 205 |
|
| 206 |
/* Atari partition is not defined for other sector sizes */ |
| 207 |
if (blkid_probe_get_sectorsize(pr) != 512) |
| 208 |
goto nothing; |
| 209 |
|
| 210 |
size = blkid_probe_get_size(pr) / 512; |
| 211 |
|
| 212 |
/* Atari is not well defined to support large disks */ |
| 213 |
if (size > INT32_MAX) |
| 214 |
goto nothing; |
| 215 |
|
| 216 |
/* read root sector */ |
| 217 |
rs = (struct atari_rootsector *) blkid_probe_get_sector(pr, 0); |
| 218 |
if (!rs) { |
| 219 |
if (errno) |
| 220 |
return -errno; |
| 221 |
goto nothing; |
| 222 |
} |
| 223 |
|
| 224 |
rssize = be32_to_cpu(rs->hd_size); |
| 225 |
|
| 226 |
/* check number of sectors stored in the root sector */ |
| 227 |
if (rssize < 2 || rssize > size) |
| 228 |
goto nothing; |
| 229 |
|
| 230 |
/* check list of bad blocks */ |
| 231 |
if ((rs->bsl_start || rs->bsl_len) |
| 232 |
&& !is_valid_dimension(be32_to_cpu(rs->bsl_start), |
| 233 |
be32_to_cpu(rs->bsl_len), |
| 234 |
rssize)) |
| 235 |
goto nothing; |
| 236 |
|
| 237 |
/* |
| 238 |
* At least one valid partition required |
| 239 |
*/ |
| 240 |
for (i = 0; i < 4; i++) { |
| 241 |
if (is_valid_partition(&rs->part[i], rssize)) { |
| 242 |
if (blkid_probe_set_magic(pr, |
| 243 |
offsetof(struct atari_rootsector, part[i]), |
| 244 |
sizeof(rs->part[i].flags) + sizeof(rs->part[i].id), |
| 245 |
(unsigned char *) &rs->part[i])) |
| 246 |
goto err; |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if (i == 4) |
| 252 |
goto nothing; |
| 253 |
|
| 254 |
if (blkid_partitions_need_typeonly(pr)) |
| 255 |
/* caller does not ask for details about partitions */ |
| 256 |
return BLKID_PROBE_OK; |
| 257 |
|
| 258 |
ls = blkid_probe_get_partlist(pr); |
| 259 |
if (!ls) |
| 260 |
goto nothing; |
| 261 |
|
| 262 |
tab = blkid_partlist_new_parttable(ls, "atari", 0); |
| 263 |
if (!tab) |
| 264 |
goto err; |
| 265 |
|
| 266 |
for (i = 0; i < ARRAY_SIZE(rs->part); i++) { |
| 267 |
struct atari_part_def *p = &rs->part[i]; |
| 268 |
|
| 269 |
if (!IS_ACTIVE(*p)) { |
| 270 |
blkid_partlist_increment_partno(ls); |
| 271 |
continue; |
| 272 |
} |
| 273 |
if (!memcmp(p->id, "XGM", 3)) { |
| 274 |
has_xgm = 1; |
| 275 |
rc = parse_extended(pr, ls, tab, p); |
| 276 |
} else { |
| 277 |
rc = parse_partition(ls, tab, p, 0); |
| 278 |
} |
| 279 |
if (rc < 0) |
| 280 |
return rc; |
| 281 |
} |
| 282 |
|
| 283 |
/* if there are no XGM partitions, we can try ICD format */ |
| 284 |
/* if first ICD partition ID is not valid, assume no ICD format */ |
| 285 |
if (!has_xgm && is_id_common(rs->icd_part[0].id)) { |
| 286 |
for (i = 0; i < ARRAY_SIZE(rs->icd_part); i++) { |
| 287 |
struct atari_part_def *p = &rs->icd_part[i]; |
| 288 |
|
| 289 |
if (!IS_ACTIVE(*p) || !is_id_common(p->id)) { |
| 290 |
blkid_partlist_increment_partno(ls); |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
rc = parse_partition(ls, tab, p, 0); |
| 295 |
if (rc < 0) |
| 296 |
return rc; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return BLKID_PROBE_OK; |
| 301 |
|
| 302 |
nothing: |
| 303 |
return BLKID_PROBE_NONE; |
| 304 |
err: |
| 305 |
return -ENOMEM; |
| 306 |
} |
| 307 |
|
| 308 |
const struct blkid_idinfo atari_pt_idinfo = |
| 309 |
{ |
| 310 |
.name = "atari", |
| 311 |
.probefunc = probe_atari_pt, |
| 312 |
.magics = BLKID_NONE_MAGIC |
| 313 |
}; |