acorn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 1996-2000 Russell King.
  4. *
  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
  8. * method.
  9. */
  10. #include <linux/buffer_head.h>
  11. #include <linux/adfs_fs.h>
  12. #include "check.h"
  13. /*
  14. * Partition types. (Oh for reusability)
  15. */
  16. #define PARTITION_RISCIX_MFM 1
  17. #define PARTITION_RISCIX_SCSI 2
  18. #define PARTITION_LINUX 9
  19. #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
  20. defined(CONFIG_ACORN_PARTITION_ADFS)
  21. static struct adfs_discrecord *
  22. adfs_partition(struct parsed_partitions *state, char *name, char *data,
  23. unsigned long first_sector, int slot)
  24. {
  25. struct adfs_discrecord *dr;
  26. unsigned int nr_sects;
  27. if (adfs_checkbblk(data))
  28. return NULL;
  29. dr = (struct adfs_discrecord *)(data + 0x1c0);
  30. if (dr->disc_size == 0 && dr->disc_size_high == 0)
  31. return NULL;
  32. nr_sects = (le32_to_cpu(dr->disc_size_high) << 23) |
  33. (le32_to_cpu(dr->disc_size) >> 9);
  34. if (name) {
  35. strlcat(state->pp_buf, " [", PAGE_SIZE);
  36. strlcat(state->pp_buf, name, PAGE_SIZE);
  37. strlcat(state->pp_buf, "]", PAGE_SIZE);
  38. }
  39. put_partition(state, slot, first_sector, nr_sects);
  40. return dr;
  41. }
  42. #endif
  43. #ifdef CONFIG_ACORN_PARTITION_RISCIX
  44. struct riscix_part {
  45. __le32 start;
  46. __le32 length;
  47. __le32 one;
  48. char name[16];
  49. };
  50. struct riscix_record {
  51. __le32 magic;
  52. #define RISCIX_MAGIC cpu_to_le32(0x4a657320)
  53. __le32 date;
  54. struct riscix_part part[8];
  55. };
  56. #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
  57. defined(CONFIG_ACORN_PARTITION_ADFS)
  58. static int riscix_partition(struct parsed_partitions *state,
  59. unsigned long first_sect, int slot,
  60. unsigned long nr_sects)
  61. {
  62. Sector sect;
  63. struct riscix_record *rr;
  64. rr = read_part_sector(state, first_sect, &sect);
  65. if (!rr)
  66. return -1;
  67. strlcat(state->pp_buf, " [RISCiX]", PAGE_SIZE);
  68. if (rr->magic == RISCIX_MAGIC) {
  69. unsigned long size = nr_sects > 2 ? 2 : nr_sects;
  70. int part;
  71. strlcat(state->pp_buf, " <", PAGE_SIZE);
  72. put_partition(state, slot++, first_sect, size);
  73. for (part = 0; part < 8; part++) {
  74. if (rr->part[part].one &&
  75. memcmp(rr->part[part].name, "All\0", 4)) {
  76. put_partition(state, slot++,
  77. le32_to_cpu(rr->part[part].start),
  78. le32_to_cpu(rr->part[part].length));
  79. strlcat(state->pp_buf, "(", PAGE_SIZE);
  80. strlcat(state->pp_buf, rr->part[part].name, PAGE_SIZE);
  81. strlcat(state->pp_buf, ")", PAGE_SIZE);
  82. }
  83. }
  84. strlcat(state->pp_buf, " >\n", PAGE_SIZE);
  85. } else {
  86. put_partition(state, slot++, first_sect, nr_sects);
  87. }
  88. put_dev_sector(sect);
  89. return slot;
  90. }
  91. #endif
  92. #endif
  93. #define LINUX_NATIVE_MAGIC 0xdeafa1de
  94. #define LINUX_SWAP_MAGIC 0xdeafab1e
  95. struct linux_part {
  96. __le32 magic;
  97. __le32 start_sect;
  98. __le32 nr_sects;
  99. };
  100. #if defined(CONFIG_ACORN_PARTITION_CUMANA) || \
  101. defined(CONFIG_ACORN_PARTITION_ADFS)
  102. static int linux_partition(struct parsed_partitions *state,
  103. unsigned long first_sect, int slot,
  104. unsigned long nr_sects)
  105. {
  106. Sector sect;
  107. struct linux_part *linuxp;
  108. unsigned long size = nr_sects > 2 ? 2 : nr_sects;
  109. strlcat(state->pp_buf, " [Linux]", PAGE_SIZE);
  110. put_partition(state, slot++, first_sect, size);
  111. linuxp = read_part_sector(state, first_sect, &sect);
  112. if (!linuxp)
  113. return -1;
  114. strlcat(state->pp_buf, " <", PAGE_SIZE);
  115. while (linuxp->magic == cpu_to_le32(LINUX_NATIVE_MAGIC) ||
  116. linuxp->magic == cpu_to_le32(LINUX_SWAP_MAGIC)) {
  117. if (slot == state->limit)
  118. break;
  119. put_partition(state, slot++, first_sect +
  120. le32_to_cpu(linuxp->start_sect),
  121. le32_to_cpu(linuxp->nr_sects));
  122. linuxp ++;
  123. }
  124. strlcat(state->pp_buf, " >", PAGE_SIZE);
  125. put_dev_sector(sect);
  126. return slot;
  127. }
  128. #endif
  129. #ifdef CONFIG_ACORN_PARTITION_CUMANA
  130. int adfspart_check_CUMANA(struct parsed_partitions *state)
  131. {
  132. unsigned long first_sector = 0;
  133. unsigned int start_blk = 0;
  134. Sector sect;
  135. unsigned char *data;
  136. char *name = "CUMANA/ADFS";
  137. int first = 1;
  138. int slot = 1;
  139. /*
  140. * Try Cumana style partitions - sector 6 contains ADFS boot block
  141. * with pointer to next 'drive'.
  142. *
  143. * There are unknowns in this code - is the 'cylinder number' of the
  144. * next partition relative to the start of this one - I'm assuming
  145. * it is.
  146. *
  147. * Also, which ID did Cumana use?
  148. *
  149. * This is totally unfinished, and will require more work to get it
  150. * going. Hence it is totally untested.
  151. */
  152. do {
  153. struct adfs_discrecord *dr;
  154. unsigned int nr_sects;
  155. data = read_part_sector(state, start_blk * 2 + 6, &sect);
  156. if (!data)
  157. return -1;
  158. if (slot == state->limit)
  159. break;
  160. dr = adfs_partition(state, name, data, first_sector, slot++);
  161. if (!dr)
  162. break;
  163. name = NULL;
  164. nr_sects = (data[0x1fd] + (data[0x1fe] << 8)) *
  165. (dr->heads + (dr->lowsector & 0x40 ? 1 : 0)) *
  166. dr->secspertrack;
  167. if (!nr_sects)
  168. break;
  169. first = 0;
  170. first_sector += nr_sects;
  171. start_blk += nr_sects >> (BLOCK_SIZE_BITS - 9);
  172. nr_sects = 0; /* hmm - should be partition size */
  173. switch (data[0x1fc] & 15) {
  174. case 0: /* No partition / ADFS? */
  175. break;
  176. #ifdef CONFIG_ACORN_PARTITION_RISCIX
  177. case PARTITION_RISCIX_SCSI:
  178. /* RISCiX - we don't know how to find the next one. */
  179. slot = riscix_partition(state, first_sector, slot,
  180. nr_sects);
  181. break;
  182. #endif
  183. case PARTITION_LINUX:
  184. slot = linux_partition(state, first_sector, slot,
  185. nr_sects);
  186. break;
  187. }
  188. put_dev_sector(sect);
  189. if (slot == -1)
  190. return -1;
  191. } while (1);
  192. put_dev_sector(sect);
  193. return first ? 0 : 1;
  194. }
  195. #endif
  196. #ifdef CONFIG_ACORN_PARTITION_ADFS
  197. /*
  198. * Purpose: allocate ADFS partitions.
  199. *
  200. * Params : hd - pointer to gendisk structure to store partition info.
  201. * dev - device number to access.
  202. *
  203. * Returns: -1 on error, 0 for no ADFS boot sector, 1 for ok.
  204. *
  205. * Alloc : hda = whole drive
  206. * hda1 = ADFS partition on first drive.
  207. * hda2 = non-ADFS partition.
  208. */
  209. int adfspart_check_ADFS(struct parsed_partitions *state)
  210. {
  211. unsigned long start_sect, nr_sects, sectscyl, heads;
  212. Sector sect;
  213. unsigned char *data;
  214. struct adfs_discrecord *dr;
  215. unsigned char id;
  216. int slot = 1;
  217. data = read_part_sector(state, 6, &sect);
  218. if (!data)
  219. return -1;
  220. dr = adfs_partition(state, "ADFS", data, 0, slot++);
  221. if (!dr) {
  222. put_dev_sector(sect);
  223. return 0;
  224. }
  225. heads = dr->heads + ((dr->lowsector >> 6) & 1);
  226. sectscyl = dr->secspertrack * heads;
  227. start_sect = ((data[0x1fe] << 8) + data[0x1fd]) * sectscyl;
  228. id = data[0x1fc] & 15;
  229. put_dev_sector(sect);
  230. /*
  231. * Work out start of non-adfs partition.
  232. */
  233. nr_sects = (state->bdev->bd_inode->i_size >> 9) - start_sect;
  234. if (start_sect) {
  235. switch (id) {
  236. #ifdef CONFIG_ACORN_PARTITION_RISCIX
  237. case PARTITION_RISCIX_SCSI:
  238. case PARTITION_RISCIX_MFM:
  239. slot = riscix_partition(state, start_sect, slot,
  240. nr_sects);
  241. break;
  242. #endif
  243. case PARTITION_LINUX:
  244. slot = linux_partition(state, start_sect, slot,
  245. nr_sects);
  246. break;
  247. }
  248. }
  249. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  250. return 1;
  251. }
  252. #endif
  253. #ifdef CONFIG_ACORN_PARTITION_ICS
  254. struct ics_part {
  255. __le32 start;
  256. __le32 size;
  257. };
  258. static int adfspart_check_ICSLinux(struct parsed_partitions *state,
  259. unsigned long block)
  260. {
  261. Sector sect;
  262. unsigned char *data = read_part_sector(state, block, &sect);
  263. int result = 0;
  264. if (data) {
  265. if (memcmp(data, "LinuxPart", 9) == 0)
  266. result = 1;
  267. put_dev_sector(sect);
  268. }
  269. return result;
  270. }
  271. /*
  272. * Check for a valid ICS partition using the checksum.
  273. */
  274. static inline int valid_ics_sector(const unsigned char *data)
  275. {
  276. unsigned long sum;
  277. int i;
  278. for (i = 0, sum = 0x50617274; i < 508; i++)
  279. sum += data[i];
  280. sum -= le32_to_cpu(*(__le32 *)(&data[508]));
  281. return sum == 0;
  282. }
  283. /*
  284. * Purpose: allocate ICS partitions.
  285. * Params : hd - pointer to gendisk structure to store partition info.
  286. * dev - device number to access.
  287. * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
  288. * Alloc : hda = whole drive
  289. * hda1 = ADFS partition 0 on first drive.
  290. * hda2 = ADFS partition 1 on first drive.
  291. * ..etc..
  292. */
  293. int adfspart_check_ICS(struct parsed_partitions *state)
  294. {
  295. const unsigned char *data;
  296. const struct ics_part *p;
  297. int slot;
  298. Sector sect;
  299. /*
  300. * Try ICS style partitions - sector 0 contains partition info.
  301. */
  302. data = read_part_sector(state, 0, &sect);
  303. if (!data)
  304. return -1;
  305. if (!valid_ics_sector(data)) {
  306. put_dev_sector(sect);
  307. return 0;
  308. }
  309. strlcat(state->pp_buf, " [ICS]", PAGE_SIZE);
  310. for (slot = 1, p = (const struct ics_part *)data; p->size; p++) {
  311. u32 start = le32_to_cpu(p->start);
  312. s32 size = le32_to_cpu(p->size); /* yes, it's signed. */
  313. if (slot == state->limit)
  314. break;
  315. /*
  316. * Negative sizes tell the RISC OS ICS driver to ignore
  317. * this partition - in effect it says that this does not
  318. * contain an ADFS filesystem.
  319. */
  320. if (size < 0) {
  321. size = -size;
  322. /*
  323. * Our own extension - We use the first sector
  324. * of the partition to identify what type this
  325. * partition is. We must not make this visible
  326. * to the filesystem.
  327. */
  328. if (size > 1 && adfspart_check_ICSLinux(state, start)) {
  329. start += 1;
  330. size -= 1;
  331. }
  332. }
  333. if (size)
  334. put_partition(state, slot++, start, size);
  335. }
  336. put_dev_sector(sect);
  337. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  338. return 1;
  339. }
  340. #endif
  341. #ifdef CONFIG_ACORN_PARTITION_POWERTEC
  342. struct ptec_part {
  343. __le32 unused1;
  344. __le32 unused2;
  345. __le32 start;
  346. __le32 size;
  347. __le32 unused5;
  348. char type[8];
  349. };
  350. static inline int valid_ptec_sector(const unsigned char *data)
  351. {
  352. unsigned char checksum = 0x2a;
  353. int i;
  354. /*
  355. * If it looks like a PC/BIOS partition, then it
  356. * probably isn't PowerTec.
  357. */
  358. if (data[510] == 0x55 && data[511] == 0xaa)
  359. return 0;
  360. for (i = 0; i < 511; i++)
  361. checksum += data[i];
  362. return checksum == data[511];
  363. }
  364. /*
  365. * Purpose: allocate ICS partitions.
  366. * Params : hd - pointer to gendisk structure to store partition info.
  367. * dev - device number to access.
  368. * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok.
  369. * Alloc : hda = whole drive
  370. * hda1 = ADFS partition 0 on first drive.
  371. * hda2 = ADFS partition 1 on first drive.
  372. * ..etc..
  373. */
  374. int adfspart_check_POWERTEC(struct parsed_partitions *state)
  375. {
  376. Sector sect;
  377. const unsigned char *data;
  378. const struct ptec_part *p;
  379. int slot = 1;
  380. int i;
  381. data = read_part_sector(state, 0, &sect);
  382. if (!data)
  383. return -1;
  384. if (!valid_ptec_sector(data)) {
  385. put_dev_sector(sect);
  386. return 0;
  387. }
  388. strlcat(state->pp_buf, " [POWERTEC]", PAGE_SIZE);
  389. for (i = 0, p = (const struct ptec_part *)data; i < 12; i++, p++) {
  390. u32 start = le32_to_cpu(p->start);
  391. u32 size = le32_to_cpu(p->size);
  392. if (size)
  393. put_partition(state, slot++, start, size);
  394. }
  395. put_dev_sector(sect);
  396. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  397. return 1;
  398. }
  399. #endif
  400. #ifdef CONFIG_ACORN_PARTITION_EESOX
  401. struct eesox_part {
  402. char magic[6];
  403. char name[10];
  404. __le32 start;
  405. __le32 unused6;
  406. __le32 unused7;
  407. __le32 unused8;
  408. };
  409. /*
  410. * Guess who created this format?
  411. */
  412. static const char eesox_name[] = {
  413. 'N', 'e', 'i', 'l', ' ',
  414. 'C', 'r', 'i', 't', 'c', 'h', 'e', 'l', 'l', ' ', ' '
  415. };
  416. /*
  417. * EESOX SCSI partition format.
  418. *
  419. * This is a goddamned awful partition format. We don't seem to store
  420. * the size of the partition in this table, only the start addresses.
  421. *
  422. * There are two possibilities where the size comes from:
  423. * 1. The individual ADFS boot block entries that are placed on the disk.
  424. * 2. The start address of the next entry.
  425. */
  426. int adfspart_check_EESOX(struct parsed_partitions *state)
  427. {
  428. Sector sect;
  429. const unsigned char *data;
  430. unsigned char buffer[256];
  431. struct eesox_part *p;
  432. sector_t start = 0;
  433. int i, slot = 1;
  434. data = read_part_sector(state, 7, &sect);
  435. if (!data)
  436. return -1;
  437. /*
  438. * "Decrypt" the partition table. God knows why...
  439. */
  440. for (i = 0; i < 256; i++)
  441. buffer[i] = data[i] ^ eesox_name[i & 15];
  442. put_dev_sector(sect);
  443. for (i = 0, p = (struct eesox_part *)buffer; i < 8; i++, p++) {
  444. sector_t next;
  445. if (memcmp(p->magic, "Eesox", 6))
  446. break;
  447. next = le32_to_cpu(p->start);
  448. if (i)
  449. put_partition(state, slot++, start, next - start);
  450. start = next;
  451. }
  452. if (i != 0) {
  453. sector_t size;
  454. size = get_capacity(state->bdev->bd_disk);
  455. put_partition(state, slot++, start, size - start);
  456. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  457. }
  458. return i ? 1 : 0;
  459. }
  460. #endif