acorn.c 12 KB

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