acorn.c 12 KB

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