ldm.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
  4. *
  5. * Copyright (C) 2001,2002 Richard Russon <[email protected]>
  6. * Copyright (c) 2001-2012 Anton Altaparmakov
  7. * Copyright (C) 2001,2002 Jakob Kemi <[email protected]>
  8. *
  9. * Documentation is available at http://www.linux-ntfs.org/doku.php?id=downloads
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/stringify.h>
  14. #include <linux/kernel.h>
  15. #include <linux/uuid.h>
  16. #include <linux/msdos_partition.h>
  17. #include "ldm.h"
  18. #include "check.h"
  19. /*
  20. * ldm_debug/info/error/crit - Output an error message
  21. * @f: A printf format string containing the message
  22. * @...: Variables to substitute into @f
  23. *
  24. * ldm_debug() writes a DEBUG level message to the syslog but only if the
  25. * driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
  26. */
  27. #ifndef CONFIG_LDM_DEBUG
  28. #define ldm_debug(...) do {} while (0)
  29. #else
  30. #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a)
  31. #endif
  32. #define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a)
  33. #define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a)
  34. #define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a)
  35. static __printf(3, 4)
  36. void _ldm_printk(const char *level, const char *function, const char *fmt, ...)
  37. {
  38. struct va_format vaf;
  39. va_list args;
  40. va_start (args, fmt);
  41. vaf.fmt = fmt;
  42. vaf.va = &args;
  43. printk("%s%s(): %pV\n", level, function, &vaf);
  44. va_end(args);
  45. }
  46. /**
  47. * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
  48. * @data: Raw database PRIVHEAD structure loaded from the device
  49. * @ph: In-memory privhead structure in which to return parsed information
  50. *
  51. * This parses the LDM database PRIVHEAD structure supplied in @data and
  52. * sets up the in-memory privhead structure @ph with the obtained information.
  53. *
  54. * Return: 'true' @ph contains the PRIVHEAD data
  55. * 'false' @ph contents are undefined
  56. */
  57. static bool ldm_parse_privhead(const u8 *data, struct privhead *ph)
  58. {
  59. bool is_vista = false;
  60. BUG_ON(!data || !ph);
  61. if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) {
  62. ldm_error("Cannot find PRIVHEAD structure. LDM database is"
  63. " corrupt. Aborting.");
  64. return false;
  65. }
  66. ph->ver_major = get_unaligned_be16(data + 0x000C);
  67. ph->ver_minor = get_unaligned_be16(data + 0x000E);
  68. ph->logical_disk_start = get_unaligned_be64(data + 0x011B);
  69. ph->logical_disk_size = get_unaligned_be64(data + 0x0123);
  70. ph->config_start = get_unaligned_be64(data + 0x012B);
  71. ph->config_size = get_unaligned_be64(data + 0x0133);
  72. /* Version 2.11 is Win2k/XP and version 2.12 is Vista. */
  73. if (ph->ver_major == 2 && ph->ver_minor == 12)
  74. is_vista = true;
  75. if (!is_vista && (ph->ver_major != 2 || ph->ver_minor != 11)) {
  76. ldm_error("Expected PRIVHEAD version 2.11 or 2.12, got %d.%d."
  77. " Aborting.", ph->ver_major, ph->ver_minor);
  78. return false;
  79. }
  80. ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major,
  81. ph->ver_minor, is_vista ? "Vista" : "2000/XP");
  82. if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
  83. /* Warn the user and continue, carefully. */
  84. ldm_info("Database is normally %u bytes, it claims to "
  85. "be %llu bytes.", LDM_DB_SIZE,
  86. (unsigned long long)ph->config_size);
  87. }
  88. if ((ph->logical_disk_size == 0) || (ph->logical_disk_start +
  89. ph->logical_disk_size > ph->config_start)) {
  90. ldm_error("PRIVHEAD disk size doesn't match real disk size");
  91. return false;
  92. }
  93. if (uuid_parse(data + 0x0030, &ph->disk_id)) {
  94. ldm_error("PRIVHEAD contains an invalid GUID.");
  95. return false;
  96. }
  97. ldm_debug("Parsed PRIVHEAD successfully.");
  98. return true;
  99. }
  100. /**
  101. * ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
  102. * @data: Raw database TOCBLOCK structure loaded from the device
  103. * @toc: In-memory toc structure in which to return parsed information
  104. *
  105. * This parses the LDM Database TOCBLOCK (table of contents) structure supplied
  106. * in @data and sets up the in-memory tocblock structure @toc with the obtained
  107. * information.
  108. *
  109. * N.B. The *_start and *_size values returned in @toc are not range-checked.
  110. *
  111. * Return: 'true' @toc contains the TOCBLOCK data
  112. * 'false' @toc contents are undefined
  113. */
  114. static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
  115. {
  116. BUG_ON (!data || !toc);
  117. if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) {
  118. ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
  119. return false;
  120. }
  121. strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
  122. toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0;
  123. toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
  124. toc->bitmap1_size = get_unaligned_be64(data + 0x36);
  125. if (strncmp (toc->bitmap1_name, TOC_BITMAP1,
  126. sizeof (toc->bitmap1_name)) != 0) {
  127. ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
  128. TOC_BITMAP1, toc->bitmap1_name);
  129. return false;
  130. }
  131. strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
  132. toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
  133. toc->bitmap2_start = get_unaligned_be64(data + 0x50);
  134. toc->bitmap2_size = get_unaligned_be64(data + 0x58);
  135. if (strncmp (toc->bitmap2_name, TOC_BITMAP2,
  136. sizeof (toc->bitmap2_name)) != 0) {
  137. ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
  138. TOC_BITMAP2, toc->bitmap2_name);
  139. return false;
  140. }
  141. ldm_debug ("Parsed TOCBLOCK successfully.");
  142. return true;
  143. }
  144. /**
  145. * ldm_parse_vmdb - Read the LDM Database VMDB structure
  146. * @data: Raw database VMDB structure loaded from the device
  147. * @vm: In-memory vmdb structure in which to return parsed information
  148. *
  149. * This parses the LDM Database VMDB structure supplied in @data and sets up
  150. * the in-memory vmdb structure @vm with the obtained information.
  151. *
  152. * N.B. The *_start, *_size and *_seq values will be range-checked later.
  153. *
  154. * Return: 'true' @vm contains VMDB info
  155. * 'false' @vm contents are undefined
  156. */
  157. static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
  158. {
  159. BUG_ON (!data || !vm);
  160. if (MAGIC_VMDB != get_unaligned_be32(data)) {
  161. ldm_crit ("Cannot find the VMDB, database may be corrupt.");
  162. return false;
  163. }
  164. vm->ver_major = get_unaligned_be16(data + 0x12);
  165. vm->ver_minor = get_unaligned_be16(data + 0x14);
  166. if ((vm->ver_major != 4) || (vm->ver_minor != 10)) {
  167. ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
  168. "Aborting.", 4, 10, vm->ver_major, vm->ver_minor);
  169. return false;
  170. }
  171. vm->vblk_size = get_unaligned_be32(data + 0x08);
  172. if (vm->vblk_size == 0) {
  173. ldm_error ("Illegal VBLK size");
  174. return false;
  175. }
  176. vm->vblk_offset = get_unaligned_be32(data + 0x0C);
  177. vm->last_vblk_seq = get_unaligned_be32(data + 0x04);
  178. ldm_debug ("Parsed VMDB successfully.");
  179. return true;
  180. }
  181. /**
  182. * ldm_compare_privheads - Compare two privhead objects
  183. * @ph1: First privhead
  184. * @ph2: Second privhead
  185. *
  186. * This compares the two privhead structures @ph1 and @ph2.
  187. *
  188. * Return: 'true' Identical
  189. * 'false' Different
  190. */
  191. static bool ldm_compare_privheads (const struct privhead *ph1,
  192. const struct privhead *ph2)
  193. {
  194. BUG_ON (!ph1 || !ph2);
  195. return ((ph1->ver_major == ph2->ver_major) &&
  196. (ph1->ver_minor == ph2->ver_minor) &&
  197. (ph1->logical_disk_start == ph2->logical_disk_start) &&
  198. (ph1->logical_disk_size == ph2->logical_disk_size) &&
  199. (ph1->config_start == ph2->config_start) &&
  200. (ph1->config_size == ph2->config_size) &&
  201. uuid_equal(&ph1->disk_id, &ph2->disk_id));
  202. }
  203. /**
  204. * ldm_compare_tocblocks - Compare two tocblock objects
  205. * @toc1: First toc
  206. * @toc2: Second toc
  207. *
  208. * This compares the two tocblock structures @toc1 and @toc2.
  209. *
  210. * Return: 'true' Identical
  211. * 'false' Different
  212. */
  213. static bool ldm_compare_tocblocks (const struct tocblock *toc1,
  214. const struct tocblock *toc2)
  215. {
  216. BUG_ON (!toc1 || !toc2);
  217. return ((toc1->bitmap1_start == toc2->bitmap1_start) &&
  218. (toc1->bitmap1_size == toc2->bitmap1_size) &&
  219. (toc1->bitmap2_start == toc2->bitmap2_start) &&
  220. (toc1->bitmap2_size == toc2->bitmap2_size) &&
  221. !strncmp (toc1->bitmap1_name, toc2->bitmap1_name,
  222. sizeof (toc1->bitmap1_name)) &&
  223. !strncmp (toc1->bitmap2_name, toc2->bitmap2_name,
  224. sizeof (toc1->bitmap2_name)));
  225. }
  226. /**
  227. * ldm_validate_privheads - Compare the primary privhead with its backups
  228. * @state: Partition check state including device holding the LDM Database
  229. * @ph1: Memory struct to fill with ph contents
  230. *
  231. * Read and compare all three privheads from disk.
  232. *
  233. * The privheads on disk show the size and location of the main disk area and
  234. * the configuration area (the database). The values are range-checked against
  235. * @hd, which contains the real size of the disk.
  236. *
  237. * Return: 'true' Success
  238. * 'false' Error
  239. */
  240. static bool ldm_validate_privheads(struct parsed_partitions *state,
  241. struct privhead *ph1)
  242. {
  243. static const int off[3] = { OFF_PRIV1, OFF_PRIV2, OFF_PRIV3 };
  244. struct privhead *ph[3] = { ph1 };
  245. Sector sect;
  246. u8 *data;
  247. bool result = false;
  248. long num_sects;
  249. int i;
  250. BUG_ON (!state || !ph1);
  251. ph[1] = kmalloc (sizeof (*ph[1]), GFP_KERNEL);
  252. ph[2] = kmalloc (sizeof (*ph[2]), GFP_KERNEL);
  253. if (!ph[1] || !ph[2]) {
  254. ldm_crit ("Out of memory.");
  255. goto out;
  256. }
  257. /* off[1 & 2] are relative to ph[0]->config_start */
  258. ph[0]->config_start = 0;
  259. /* Read and parse privheads */
  260. for (i = 0; i < 3; i++) {
  261. data = read_part_sector(state, ph[0]->config_start + off[i],
  262. &sect);
  263. if (!data) {
  264. ldm_crit ("Disk read failed.");
  265. goto out;
  266. }
  267. result = ldm_parse_privhead (data, ph[i]);
  268. put_dev_sector (sect);
  269. if (!result) {
  270. ldm_error ("Cannot find PRIVHEAD %d.", i+1); /* Log again */
  271. if (i < 2)
  272. goto out; /* Already logged */
  273. else
  274. break; /* FIXME ignore for now, 3rd PH can fail on odd-sized disks */
  275. }
  276. }
  277. num_sects = get_capacity(state->disk);
  278. if ((ph[0]->config_start > num_sects) ||
  279. ((ph[0]->config_start + ph[0]->config_size) > num_sects)) {
  280. ldm_crit ("Database extends beyond the end of the disk.");
  281. goto out;
  282. }
  283. if ((ph[0]->logical_disk_start > ph[0]->config_start) ||
  284. ((ph[0]->logical_disk_start + ph[0]->logical_disk_size)
  285. > ph[0]->config_start)) {
  286. ldm_crit ("Disk and database overlap.");
  287. goto out;
  288. }
  289. if (!ldm_compare_privheads (ph[0], ph[1])) {
  290. ldm_crit ("Primary and backup PRIVHEADs don't match.");
  291. goto out;
  292. }
  293. /* FIXME ignore this for now
  294. if (!ldm_compare_privheads (ph[0], ph[2])) {
  295. ldm_crit ("Primary and backup PRIVHEADs don't match.");
  296. goto out;
  297. }*/
  298. ldm_debug ("Validated PRIVHEADs successfully.");
  299. result = true;
  300. out:
  301. kfree (ph[1]);
  302. kfree (ph[2]);
  303. return result;
  304. }
  305. /**
  306. * ldm_validate_tocblocks - Validate the table of contents and its backups
  307. * @state: Partition check state including device holding the LDM Database
  308. * @base: Offset, into @state->disk, of the database
  309. * @ldb: Cache of the database structures
  310. *
  311. * Find and compare the four tables of contents of the LDM Database stored on
  312. * @state->disk and return the parsed information into @toc1.
  313. *
  314. * The offsets and sizes of the configs are range-checked against a privhead.
  315. *
  316. * Return: 'true' @toc1 contains validated TOCBLOCK info
  317. * 'false' @toc1 contents are undefined
  318. */
  319. static bool ldm_validate_tocblocks(struct parsed_partitions *state,
  320. unsigned long base, struct ldmdb *ldb)
  321. {
  322. static const int off[4] = { OFF_TOCB1, OFF_TOCB2, OFF_TOCB3, OFF_TOCB4};
  323. struct tocblock *tb[4];
  324. struct privhead *ph;
  325. Sector sect;
  326. u8 *data;
  327. int i, nr_tbs;
  328. bool result = false;
  329. BUG_ON(!state || !ldb);
  330. ph = &ldb->ph;
  331. tb[0] = &ldb->toc;
  332. tb[1] = kmalloc_array(3, sizeof(*tb[1]), GFP_KERNEL);
  333. if (!tb[1]) {
  334. ldm_crit("Out of memory.");
  335. goto err;
  336. }
  337. tb[2] = (struct tocblock*)((u8*)tb[1] + sizeof(*tb[1]));
  338. tb[3] = (struct tocblock*)((u8*)tb[2] + sizeof(*tb[2]));
  339. /*
  340. * Try to read and parse all four TOCBLOCKs.
  341. *
  342. * Windows Vista LDM v2.12 does not always have all four TOCBLOCKs so
  343. * skip any that fail as long as we get at least one valid TOCBLOCK.
  344. */
  345. for (nr_tbs = i = 0; i < 4; i++) {
  346. data = read_part_sector(state, base + off[i], &sect);
  347. if (!data) {
  348. ldm_error("Disk read failed for TOCBLOCK %d.", i);
  349. continue;
  350. }
  351. if (ldm_parse_tocblock(data, tb[nr_tbs]))
  352. nr_tbs++;
  353. put_dev_sector(sect);
  354. }
  355. if (!nr_tbs) {
  356. ldm_crit("Failed to find a valid TOCBLOCK.");
  357. goto err;
  358. }
  359. /* Range check the TOCBLOCK against a privhead. */
  360. if (((tb[0]->bitmap1_start + tb[0]->bitmap1_size) > ph->config_size) ||
  361. ((tb[0]->bitmap2_start + tb[0]->bitmap2_size) >
  362. ph->config_size)) {
  363. ldm_crit("The bitmaps are out of range. Giving up.");
  364. goto err;
  365. }
  366. /* Compare all loaded TOCBLOCKs. */
  367. for (i = 1; i < nr_tbs; i++) {
  368. if (!ldm_compare_tocblocks(tb[0], tb[i])) {
  369. ldm_crit("TOCBLOCKs 0 and %d do not match.", i);
  370. goto err;
  371. }
  372. }
  373. ldm_debug("Validated %d TOCBLOCKs successfully.", nr_tbs);
  374. result = true;
  375. err:
  376. kfree(tb[1]);
  377. return result;
  378. }
  379. /**
  380. * ldm_validate_vmdb - Read the VMDB and validate it
  381. * @state: Partition check state including device holding the LDM Database
  382. * @base: Offset, into @bdev, of the database
  383. * @ldb: Cache of the database structures
  384. *
  385. * Find the vmdb of the LDM Database stored on @bdev and return the parsed
  386. * information in @ldb.
  387. *
  388. * Return: 'true' @ldb contains validated VBDB info
  389. * 'false' @ldb contents are undefined
  390. */
  391. static bool ldm_validate_vmdb(struct parsed_partitions *state,
  392. unsigned long base, struct ldmdb *ldb)
  393. {
  394. Sector sect;
  395. u8 *data;
  396. bool result = false;
  397. struct vmdb *vm;
  398. struct tocblock *toc;
  399. BUG_ON (!state || !ldb);
  400. vm = &ldb->vm;
  401. toc = &ldb->toc;
  402. data = read_part_sector(state, base + OFF_VMDB, &sect);
  403. if (!data) {
  404. ldm_crit ("Disk read failed.");
  405. return false;
  406. }
  407. if (!ldm_parse_vmdb (data, vm))
  408. goto out; /* Already logged */
  409. /* Are there uncommitted transactions? */
  410. if (get_unaligned_be16(data + 0x10) != 0x01) {
  411. ldm_crit ("Database is not in a consistent state. Aborting.");
  412. goto out;
  413. }
  414. if (vm->vblk_offset != 512)
  415. ldm_info ("VBLKs start at offset 0x%04x.", vm->vblk_offset);
  416. /*
  417. * The last_vblkd_seq can be before the end of the vmdb, just make sure
  418. * it is not out of bounds.
  419. */
  420. if ((vm->vblk_size * vm->last_vblk_seq) > (toc->bitmap1_size << 9)) {
  421. ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
  422. "Database is corrupt. Aborting.");
  423. goto out;
  424. }
  425. result = true;
  426. out:
  427. put_dev_sector (sect);
  428. return result;
  429. }
  430. /**
  431. * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
  432. * @state: Partition check state including device holding the LDM Database
  433. *
  434. * This function provides a weak test to decide whether the device is a dynamic
  435. * disk or not. It looks for an MS-DOS-style partition table containing at
  436. * least one partition of type 0x42 (formerly SFS, now used by Windows for
  437. * dynamic disks).
  438. *
  439. * N.B. The only possible error can come from the read_part_sector and that is
  440. * only likely to happen if the underlying device is strange. If that IS
  441. * the case we should return zero to let someone else try.
  442. *
  443. * Return: 'true' @state->disk is a dynamic disk
  444. * 'false' @state->disk is not a dynamic disk, or an error occurred
  445. */
  446. static bool ldm_validate_partition_table(struct parsed_partitions *state)
  447. {
  448. Sector sect;
  449. u8 *data;
  450. struct msdos_partition *p;
  451. int i;
  452. bool result = false;
  453. BUG_ON(!state);
  454. data = read_part_sector(state, 0, &sect);
  455. if (!data) {
  456. ldm_info ("Disk read failed.");
  457. return false;
  458. }
  459. if (*(__le16*) (data + 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC))
  460. goto out;
  461. p = (struct msdos_partition *)(data + 0x01BE);
  462. for (i = 0; i < 4; i++, p++)
  463. if (p->sys_ind == LDM_PARTITION) {
  464. result = true;
  465. break;
  466. }
  467. if (result)
  468. ldm_debug ("Found W2K dynamic disk partition type.");
  469. out:
  470. put_dev_sector (sect);
  471. return result;
  472. }
  473. /**
  474. * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
  475. * @ldb: Cache of the database structures
  476. *
  477. * The LDM Database contains a list of all partitions on all dynamic disks.
  478. * The primary PRIVHEAD, at the beginning of the physical disk, tells us
  479. * the GUID of this disk. This function searches for the GUID in a linked
  480. * list of vblk's.
  481. *
  482. * Return: Pointer, A matching vblk was found
  483. * NULL, No match, or an error
  484. */
  485. static struct vblk * ldm_get_disk_objid (const struct ldmdb *ldb)
  486. {
  487. struct list_head *item;
  488. BUG_ON (!ldb);
  489. list_for_each (item, &ldb->v_disk) {
  490. struct vblk *v = list_entry (item, struct vblk, list);
  491. if (uuid_equal(&v->vblk.disk.disk_id, &ldb->ph.disk_id))
  492. return v;
  493. }
  494. return NULL;
  495. }
  496. /**
  497. * ldm_create_data_partitions - Create data partitions for this device
  498. * @pp: List of the partitions parsed so far
  499. * @ldb: Cache of the database structures
  500. *
  501. * The database contains ALL the partitions for ALL disk groups, so we need to
  502. * filter out this specific disk. Using the disk's object id, we can find all
  503. * the partitions in the database that belong to this disk.
  504. *
  505. * Add each partition in our database, to the parsed_partitions structure.
  506. *
  507. * N.B. This function creates the partitions in the order it finds partition
  508. * objects in the linked list.
  509. *
  510. * Return: 'true' Partition created
  511. * 'false' Error, probably a range checking problem
  512. */
  513. static bool ldm_create_data_partitions (struct parsed_partitions *pp,
  514. const struct ldmdb *ldb)
  515. {
  516. struct list_head *item;
  517. struct vblk *vb;
  518. struct vblk *disk;
  519. struct vblk_part *part;
  520. int part_num = 1;
  521. BUG_ON (!pp || !ldb);
  522. disk = ldm_get_disk_objid (ldb);
  523. if (!disk) {
  524. ldm_crit ("Can't find the ID of this disk in the database.");
  525. return false;
  526. }
  527. strlcat(pp->pp_buf, " [LDM]", PAGE_SIZE);
  528. /* Create the data partitions */
  529. list_for_each (item, &ldb->v_part) {
  530. vb = list_entry (item, struct vblk, list);
  531. part = &vb->vblk.part;
  532. if (part->disk_id != disk->obj_id)
  533. continue;
  534. put_partition (pp, part_num, ldb->ph.logical_disk_start +
  535. part->start, part->size);
  536. part_num++;
  537. }
  538. strlcat(pp->pp_buf, "\n", PAGE_SIZE);
  539. return true;
  540. }
  541. /**
  542. * ldm_relative - Calculate the next relative offset
  543. * @buffer: Block of data being worked on
  544. * @buflen: Size of the block of data
  545. * @base: Size of the previous fixed width fields
  546. * @offset: Cumulative size of the previous variable-width fields
  547. *
  548. * Because many of the VBLK fields are variable-width, it's necessary
  549. * to calculate each offset based on the previous one and the length
  550. * of the field it pointed to.
  551. *
  552. * Return: -1 Error, the calculated offset exceeded the size of the buffer
  553. * n OK, a range-checked offset into buffer
  554. */
  555. static int ldm_relative(const u8 *buffer, int buflen, int base, int offset)
  556. {
  557. base += offset;
  558. if (!buffer || offset < 0 || base > buflen) {
  559. if (!buffer)
  560. ldm_error("!buffer");
  561. if (offset < 0)
  562. ldm_error("offset (%d) < 0", offset);
  563. if (base > buflen)
  564. ldm_error("base (%d) > buflen (%d)", base, buflen);
  565. return -1;
  566. }
  567. if (base + buffer[base] >= buflen) {
  568. ldm_error("base (%d) + buffer[base] (%d) >= buflen (%d)", base,
  569. buffer[base], buflen);
  570. return -1;
  571. }
  572. return buffer[base] + offset + 1;
  573. }
  574. /**
  575. * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
  576. * @block: Pointer to the variable-width number to convert
  577. *
  578. * Large numbers in the LDM Database are often stored in a packed format. Each
  579. * number is prefixed by a one byte width marker. All numbers in the database
  580. * are stored in big-endian byte order. This function reads one of these
  581. * numbers and returns the result
  582. *
  583. * N.B. This function DOES NOT perform any range checking, though the most
  584. * it will read is eight bytes.
  585. *
  586. * Return: n A number
  587. * 0 Zero, or an error occurred
  588. */
  589. static u64 ldm_get_vnum (const u8 *block)
  590. {
  591. u64 tmp = 0;
  592. u8 length;
  593. BUG_ON (!block);
  594. length = *block++;
  595. if (length && length <= 8)
  596. while (length--)
  597. tmp = (tmp << 8) | *block++;
  598. else
  599. ldm_error ("Illegal length %d.", length);
  600. return tmp;
  601. }
  602. /**
  603. * ldm_get_vstr - Read a length-prefixed string into a buffer
  604. * @block: Pointer to the length marker
  605. * @buffer: Location to copy string to
  606. * @buflen: Size of the output buffer
  607. *
  608. * Many of the strings in the LDM Database are not NULL terminated. Instead
  609. * they are prefixed by a one byte length marker. This function copies one of
  610. * these strings into a buffer.
  611. *
  612. * N.B. This function DOES NOT perform any range checking on the input.
  613. * If the buffer is too small, the output will be truncated.
  614. *
  615. * Return: 0, Error and @buffer contents are undefined
  616. * n, String length in characters (excluding NULL)
  617. * buflen-1, String was truncated.
  618. */
  619. static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen)
  620. {
  621. int length;
  622. BUG_ON (!block || !buffer);
  623. length = block[0];
  624. if (length >= buflen) {
  625. ldm_error ("Truncating string %d -> %d.", length, buflen);
  626. length = buflen - 1;
  627. }
  628. memcpy (buffer, block + 1, length);
  629. buffer[length] = 0;
  630. return length;
  631. }
  632. /**
  633. * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure
  634. * @buffer: Block of data being worked on
  635. * @buflen: Size of the block of data
  636. * @vb: In-memory vblk in which to return information
  637. *
  638. * Read a raw VBLK Component object (version 3) into a vblk structure.
  639. *
  640. * Return: 'true' @vb contains a Component VBLK
  641. * 'false' @vb contents are not defined
  642. */
  643. static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb)
  644. {
  645. int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len;
  646. struct vblk_comp *comp;
  647. BUG_ON (!buffer || !vb);
  648. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  649. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  650. r_vstate = ldm_relative (buffer, buflen, 0x18, r_name);
  651. r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate);
  652. r_parent = ldm_relative (buffer, buflen, 0x2D, r_child);
  653. if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) {
  654. r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent);
  655. r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe);
  656. len = r_cols;
  657. } else {
  658. r_stripe = 0;
  659. len = r_parent;
  660. }
  661. if (len < 0)
  662. return false;
  663. len += VBLK_SIZE_CMP3;
  664. if (len != get_unaligned_be32(buffer + 0x14))
  665. return false;
  666. comp = &vb->vblk.comp;
  667. ldm_get_vstr (buffer + 0x18 + r_name, comp->state,
  668. sizeof (comp->state));
  669. comp->type = buffer[0x18 + r_vstate];
  670. comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate);
  671. comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child);
  672. comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0;
  673. return true;
  674. }
  675. /**
  676. * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
  677. * @buffer: Block of data being worked on
  678. * @buflen: Size of the block of data
  679. * @vb: In-memory vblk in which to return information
  680. *
  681. * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
  682. *
  683. * Return: 'true' @vb contains a Disk Group VBLK
  684. * 'false' @vb contents are not defined
  685. */
  686. static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb)
  687. {
  688. int r_objid, r_name, r_diskid, r_id1, r_id2, len;
  689. struct vblk_dgrp *dgrp;
  690. BUG_ON (!buffer || !vb);
  691. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  692. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  693. r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
  694. if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) {
  695. r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid);
  696. r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1);
  697. len = r_id2;
  698. } else
  699. len = r_diskid;
  700. if (len < 0)
  701. return false;
  702. len += VBLK_SIZE_DGR3;
  703. if (len != get_unaligned_be32(buffer + 0x14))
  704. return false;
  705. dgrp = &vb->vblk.dgrp;
  706. ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id,
  707. sizeof (dgrp->disk_id));
  708. return true;
  709. }
  710. /**
  711. * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
  712. * @buffer: Block of data being worked on
  713. * @buflen: Size of the block of data
  714. * @vb: In-memory vblk in which to return information
  715. *
  716. * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
  717. *
  718. * Return: 'true' @vb contains a Disk Group VBLK
  719. * 'false' @vb contents are not defined
  720. */
  721. static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb)
  722. {
  723. char buf[64];
  724. int r_objid, r_name, r_id1, r_id2, len;
  725. BUG_ON (!buffer || !vb);
  726. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  727. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  728. if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) {
  729. r_id1 = ldm_relative (buffer, buflen, 0x44, r_name);
  730. r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1);
  731. len = r_id2;
  732. } else
  733. len = r_name;
  734. if (len < 0)
  735. return false;
  736. len += VBLK_SIZE_DGR4;
  737. if (len != get_unaligned_be32(buffer + 0x14))
  738. return false;
  739. ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf));
  740. return true;
  741. }
  742. /**
  743. * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
  744. * @buffer: Block of data being worked on
  745. * @buflen: Size of the block of data
  746. * @vb: In-memory vblk in which to return information
  747. *
  748. * Read a raw VBLK Disk object (version 3) into a vblk structure.
  749. *
  750. * Return: 'true' @vb contains a Disk VBLK
  751. * 'false' @vb contents are not defined
  752. */
  753. static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb)
  754. {
  755. int r_objid, r_name, r_diskid, r_altname, len;
  756. struct vblk_disk *disk;
  757. BUG_ON (!buffer || !vb);
  758. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  759. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  760. r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
  761. r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid);
  762. len = r_altname;
  763. if (len < 0)
  764. return false;
  765. len += VBLK_SIZE_DSK3;
  766. if (len != get_unaligned_be32(buffer + 0x14))
  767. return false;
  768. disk = &vb->vblk.disk;
  769. ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name,
  770. sizeof (disk->alt_name));
  771. if (uuid_parse(buffer + 0x19 + r_name, &disk->disk_id))
  772. return false;
  773. return true;
  774. }
  775. /**
  776. * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
  777. * @buffer: Block of data being worked on
  778. * @buflen: Size of the block of data
  779. * @vb: In-memory vblk in which to return information
  780. *
  781. * Read a raw VBLK Disk object (version 4) into a vblk structure.
  782. *
  783. * Return: 'true' @vb contains a Disk VBLK
  784. * 'false' @vb contents are not defined
  785. */
  786. static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb)
  787. {
  788. int r_objid, r_name, len;
  789. struct vblk_disk *disk;
  790. BUG_ON (!buffer || !vb);
  791. r_objid = ldm_relative (buffer, buflen, 0x18, 0);
  792. r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
  793. len = r_name;
  794. if (len < 0)
  795. return false;
  796. len += VBLK_SIZE_DSK4;
  797. if (len != get_unaligned_be32(buffer + 0x14))
  798. return false;
  799. disk = &vb->vblk.disk;
  800. import_uuid(&disk->disk_id, buffer + 0x18 + r_name);
  801. return true;
  802. }
  803. /**
  804. * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
  805. * @buffer: Block of data being worked on
  806. * @buflen: Size of the block of data
  807. * @vb: In-memory vblk in which to return information
  808. *
  809. * Read a raw VBLK Partition object (version 3) into a vblk structure.
  810. *
  811. * Return: 'true' @vb contains a Partition VBLK
  812. * 'false' @vb contents are not defined
  813. */
  814. static bool ldm_parse_prt3(const u8 *buffer, int buflen, struct vblk *vb)
  815. {
  816. int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len;
  817. struct vblk_part *part;
  818. BUG_ON(!buffer || !vb);
  819. r_objid = ldm_relative(buffer, buflen, 0x18, 0);
  820. if (r_objid < 0) {
  821. ldm_error("r_objid %d < 0", r_objid);
  822. return false;
  823. }
  824. r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
  825. if (r_name < 0) {
  826. ldm_error("r_name %d < 0", r_name);
  827. return false;
  828. }
  829. r_size = ldm_relative(buffer, buflen, 0x34, r_name);
  830. if (r_size < 0) {
  831. ldm_error("r_size %d < 0", r_size);
  832. return false;
  833. }
  834. r_parent = ldm_relative(buffer, buflen, 0x34, r_size);
  835. if (r_parent < 0) {
  836. ldm_error("r_parent %d < 0", r_parent);
  837. return false;
  838. }
  839. r_diskid = ldm_relative(buffer, buflen, 0x34, r_parent);
  840. if (r_diskid < 0) {
  841. ldm_error("r_diskid %d < 0", r_diskid);
  842. return false;
  843. }
  844. if (buffer[0x12] & VBLK_FLAG_PART_INDEX) {
  845. r_index = ldm_relative(buffer, buflen, 0x34, r_diskid);
  846. if (r_index < 0) {
  847. ldm_error("r_index %d < 0", r_index);
  848. return false;
  849. }
  850. len = r_index;
  851. } else
  852. len = r_diskid;
  853. if (len < 0) {
  854. ldm_error("len %d < 0", len);
  855. return false;
  856. }
  857. len += VBLK_SIZE_PRT3;
  858. if (len > get_unaligned_be32(buffer + 0x14)) {
  859. ldm_error("len %d > BE32(buffer + 0x14) %d", len,
  860. get_unaligned_be32(buffer + 0x14));
  861. return false;
  862. }
  863. part = &vb->vblk.part;
  864. part->start = get_unaligned_be64(buffer + 0x24 + r_name);
  865. part->volume_offset = get_unaligned_be64(buffer + 0x2C + r_name);
  866. part->size = ldm_get_vnum(buffer + 0x34 + r_name);
  867. part->parent_id = ldm_get_vnum(buffer + 0x34 + r_size);
  868. part->disk_id = ldm_get_vnum(buffer + 0x34 + r_parent);
  869. if (vb->flags & VBLK_FLAG_PART_INDEX)
  870. part->partnum = buffer[0x35 + r_diskid];
  871. else
  872. part->partnum = 0;
  873. return true;
  874. }
  875. /**
  876. * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
  877. * @buffer: Block of data being worked on
  878. * @buflen: Size of the block of data
  879. * @vb: In-memory vblk in which to return information
  880. *
  881. * Read a raw VBLK Volume object (version 5) into a vblk structure.
  882. *
  883. * Return: 'true' @vb contains a Volume VBLK
  884. * 'false' @vb contents are not defined
  885. */
  886. static bool ldm_parse_vol5(const u8 *buffer, int buflen, struct vblk *vb)
  887. {
  888. int r_objid, r_name, r_vtype, r_disable_drive_letter, r_child, r_size;
  889. int r_id1, r_id2, r_size2, r_drive, len;
  890. struct vblk_volu *volu;
  891. BUG_ON(!buffer || !vb);
  892. r_objid = ldm_relative(buffer, buflen, 0x18, 0);
  893. if (r_objid < 0) {
  894. ldm_error("r_objid %d < 0", r_objid);
  895. return false;
  896. }
  897. r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
  898. if (r_name < 0) {
  899. ldm_error("r_name %d < 0", r_name);
  900. return false;
  901. }
  902. r_vtype = ldm_relative(buffer, buflen, 0x18, r_name);
  903. if (r_vtype < 0) {
  904. ldm_error("r_vtype %d < 0", r_vtype);
  905. return false;
  906. }
  907. r_disable_drive_letter = ldm_relative(buffer, buflen, 0x18, r_vtype);
  908. if (r_disable_drive_letter < 0) {
  909. ldm_error("r_disable_drive_letter %d < 0",
  910. r_disable_drive_letter);
  911. return false;
  912. }
  913. r_child = ldm_relative(buffer, buflen, 0x2D, r_disable_drive_letter);
  914. if (r_child < 0) {
  915. ldm_error("r_child %d < 0", r_child);
  916. return false;
  917. }
  918. r_size = ldm_relative(buffer, buflen, 0x3D, r_child);
  919. if (r_size < 0) {
  920. ldm_error("r_size %d < 0", r_size);
  921. return false;
  922. }
  923. if (buffer[0x12] & VBLK_FLAG_VOLU_ID1) {
  924. r_id1 = ldm_relative(buffer, buflen, 0x52, r_size);
  925. if (r_id1 < 0) {
  926. ldm_error("r_id1 %d < 0", r_id1);
  927. return false;
  928. }
  929. } else
  930. r_id1 = r_size;
  931. if (buffer[0x12] & VBLK_FLAG_VOLU_ID2) {
  932. r_id2 = ldm_relative(buffer, buflen, 0x52, r_id1);
  933. if (r_id2 < 0) {
  934. ldm_error("r_id2 %d < 0", r_id2);
  935. return false;
  936. }
  937. } else
  938. r_id2 = r_id1;
  939. if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE) {
  940. r_size2 = ldm_relative(buffer, buflen, 0x52, r_id2);
  941. if (r_size2 < 0) {
  942. ldm_error("r_size2 %d < 0", r_size2);
  943. return false;
  944. }
  945. } else
  946. r_size2 = r_id2;
  947. if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
  948. r_drive = ldm_relative(buffer, buflen, 0x52, r_size2);
  949. if (r_drive < 0) {
  950. ldm_error("r_drive %d < 0", r_drive);
  951. return false;
  952. }
  953. } else
  954. r_drive = r_size2;
  955. len = r_drive;
  956. if (len < 0) {
  957. ldm_error("len %d < 0", len);
  958. return false;
  959. }
  960. len += VBLK_SIZE_VOL5;
  961. if (len > get_unaligned_be32(buffer + 0x14)) {
  962. ldm_error("len %d > BE32(buffer + 0x14) %d", len,
  963. get_unaligned_be32(buffer + 0x14));
  964. return false;
  965. }
  966. volu = &vb->vblk.volu;
  967. ldm_get_vstr(buffer + 0x18 + r_name, volu->volume_type,
  968. sizeof(volu->volume_type));
  969. memcpy(volu->volume_state, buffer + 0x18 + r_disable_drive_letter,
  970. sizeof(volu->volume_state));
  971. volu->size = ldm_get_vnum(buffer + 0x3D + r_child);
  972. volu->partition_type = buffer[0x41 + r_size];
  973. memcpy(volu->guid, buffer + 0x42 + r_size, sizeof(volu->guid));
  974. if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
  975. ldm_get_vstr(buffer + 0x52 + r_size, volu->drive_hint,
  976. sizeof(volu->drive_hint));
  977. }
  978. return true;
  979. }
  980. /**
  981. * ldm_parse_vblk - Read a raw VBLK object into a vblk structure
  982. * @buf: Block of data being worked on
  983. * @len: Size of the block of data
  984. * @vb: In-memory vblk in which to return information
  985. *
  986. * Read a raw VBLK object into a vblk structure. This function just reads the
  987. * information common to all VBLK types, then delegates the rest of the work to
  988. * helper functions: ldm_parse_*.
  989. *
  990. * Return: 'true' @vb contains a VBLK
  991. * 'false' @vb contents are not defined
  992. */
  993. static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb)
  994. {
  995. bool result = false;
  996. int r_objid;
  997. BUG_ON (!buf || !vb);
  998. r_objid = ldm_relative (buf, len, 0x18, 0);
  999. if (r_objid < 0) {
  1000. ldm_error ("VBLK header is corrupt.");
  1001. return false;
  1002. }
  1003. vb->flags = buf[0x12];
  1004. vb->type = buf[0x13];
  1005. vb->obj_id = ldm_get_vnum (buf + 0x18);
  1006. ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name));
  1007. switch (vb->type) {
  1008. case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break;
  1009. case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break;
  1010. case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break;
  1011. case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break;
  1012. case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break;
  1013. case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break;
  1014. case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break;
  1015. }
  1016. if (result)
  1017. ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.",
  1018. (unsigned long long) vb->obj_id, vb->type);
  1019. else
  1020. ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).",
  1021. (unsigned long long) vb->obj_id, vb->type);
  1022. return result;
  1023. }
  1024. /**
  1025. * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database
  1026. * @data: Raw VBLK to add to the database
  1027. * @len: Size of the raw VBLK
  1028. * @ldb: Cache of the database structures
  1029. *
  1030. * The VBLKs are sorted into categories. Partitions are also sorted by offset.
  1031. *
  1032. * N.B. This function does not check the validity of the VBLKs.
  1033. *
  1034. * Return: 'true' The VBLK was added
  1035. * 'false' An error occurred
  1036. */
  1037. static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb)
  1038. {
  1039. struct vblk *vb;
  1040. struct list_head *item;
  1041. BUG_ON (!data || !ldb);
  1042. vb = kmalloc (sizeof (*vb), GFP_KERNEL);
  1043. if (!vb) {
  1044. ldm_crit ("Out of memory.");
  1045. return false;
  1046. }
  1047. if (!ldm_parse_vblk (data, len, vb)) {
  1048. kfree(vb);
  1049. return false; /* Already logged */
  1050. }
  1051. /* Put vblk into the correct list. */
  1052. switch (vb->type) {
  1053. case VBLK_DGR3:
  1054. case VBLK_DGR4:
  1055. list_add (&vb->list, &ldb->v_dgrp);
  1056. break;
  1057. case VBLK_DSK3:
  1058. case VBLK_DSK4:
  1059. list_add (&vb->list, &ldb->v_disk);
  1060. break;
  1061. case VBLK_VOL5:
  1062. list_add (&vb->list, &ldb->v_volu);
  1063. break;
  1064. case VBLK_CMP3:
  1065. list_add (&vb->list, &ldb->v_comp);
  1066. break;
  1067. case VBLK_PRT3:
  1068. /* Sort by the partition's start sector. */
  1069. list_for_each (item, &ldb->v_part) {
  1070. struct vblk *v = list_entry (item, struct vblk, list);
  1071. if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) &&
  1072. (v->vblk.part.start > vb->vblk.part.start)) {
  1073. list_add_tail (&vb->list, &v->list);
  1074. return true;
  1075. }
  1076. }
  1077. list_add_tail (&vb->list, &ldb->v_part);
  1078. break;
  1079. }
  1080. return true;
  1081. }
  1082. /**
  1083. * ldm_frag_add - Add a VBLK fragment to a list
  1084. * @data: Raw fragment to be added to the list
  1085. * @size: Size of the raw fragment
  1086. * @frags: Linked list of VBLK fragments
  1087. *
  1088. * Fragmented VBLKs may not be consecutive in the database, so they are placed
  1089. * in a list so they can be pieced together later.
  1090. *
  1091. * Return: 'true' Success, the VBLK was added to the list
  1092. * 'false' Error, a problem occurred
  1093. */
  1094. static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags)
  1095. {
  1096. struct frag *f;
  1097. struct list_head *item;
  1098. int rec, num, group;
  1099. BUG_ON (!data || !frags);
  1100. if (size < 2 * VBLK_SIZE_HEAD) {
  1101. ldm_error("Value of size is too small.");
  1102. return false;
  1103. }
  1104. group = get_unaligned_be32(data + 0x08);
  1105. rec = get_unaligned_be16(data + 0x0C);
  1106. num = get_unaligned_be16(data + 0x0E);
  1107. if ((num < 1) || (num > 4)) {
  1108. ldm_error ("A VBLK claims to have %d parts.", num);
  1109. return false;
  1110. }
  1111. if (rec >= num) {
  1112. ldm_error("REC value (%d) exceeds NUM value (%d)", rec, num);
  1113. return false;
  1114. }
  1115. list_for_each (item, frags) {
  1116. f = list_entry (item, struct frag, list);
  1117. if (f->group == group)
  1118. goto found;
  1119. }
  1120. f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL);
  1121. if (!f) {
  1122. ldm_crit ("Out of memory.");
  1123. return false;
  1124. }
  1125. f->group = group;
  1126. f->num = num;
  1127. f->rec = rec;
  1128. f->map = 0xFF << num;
  1129. list_add_tail (&f->list, frags);
  1130. found:
  1131. if (rec >= f->num) {
  1132. ldm_error("REC value (%d) exceeds NUM value (%d)", rec, f->num);
  1133. return false;
  1134. }
  1135. if (f->map & (1 << rec)) {
  1136. ldm_error ("Duplicate VBLK, part %d.", rec);
  1137. f->map &= 0x7F; /* Mark the group as broken */
  1138. return false;
  1139. }
  1140. f->map |= (1 << rec);
  1141. if (!rec)
  1142. memcpy(f->data, data, VBLK_SIZE_HEAD);
  1143. data += VBLK_SIZE_HEAD;
  1144. size -= VBLK_SIZE_HEAD;
  1145. memcpy(f->data + VBLK_SIZE_HEAD + rec * size, data, size);
  1146. return true;
  1147. }
  1148. /**
  1149. * ldm_frag_free - Free a linked list of VBLK fragments
  1150. * @list: Linked list of fragments
  1151. *
  1152. * Free a linked list of VBLK fragments
  1153. *
  1154. * Return: none
  1155. */
  1156. static void ldm_frag_free (struct list_head *list)
  1157. {
  1158. struct list_head *item, *tmp;
  1159. BUG_ON (!list);
  1160. list_for_each_safe (item, tmp, list)
  1161. kfree (list_entry (item, struct frag, list));
  1162. }
  1163. /**
  1164. * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
  1165. * @frags: Linked list of VBLK fragments
  1166. * @ldb: Cache of the database structures
  1167. *
  1168. * Now that all the fragmented VBLKs have been collected, they must be added to
  1169. * the database for later use.
  1170. *
  1171. * Return: 'true' All the fragments we added successfully
  1172. * 'false' One or more of the fragments we invalid
  1173. */
  1174. static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
  1175. {
  1176. struct frag *f;
  1177. struct list_head *item;
  1178. BUG_ON (!frags || !ldb);
  1179. list_for_each (item, frags) {
  1180. f = list_entry (item, struct frag, list);
  1181. if (f->map != 0xFF) {
  1182. ldm_error ("VBLK group %d is incomplete (0x%02x).",
  1183. f->group, f->map);
  1184. return false;
  1185. }
  1186. if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb))
  1187. return false; /* Already logged */
  1188. }
  1189. return true;
  1190. }
  1191. /**
  1192. * ldm_get_vblks - Read the on-disk database of VBLKs into memory
  1193. * @state: Partition check state including device holding the LDM Database
  1194. * @base: Offset, into @state->disk, of the database
  1195. * @ldb: Cache of the database structures
  1196. *
  1197. * To use the information from the VBLKs, they need to be read from the disk,
  1198. * unpacked and validated. We cache them in @ldb according to their type.
  1199. *
  1200. * Return: 'true' All the VBLKs were read successfully
  1201. * 'false' An error occurred
  1202. */
  1203. static bool ldm_get_vblks(struct parsed_partitions *state, unsigned long base,
  1204. struct ldmdb *ldb)
  1205. {
  1206. int size, perbuf, skip, finish, s, v, recs;
  1207. u8 *data = NULL;
  1208. Sector sect;
  1209. bool result = false;
  1210. LIST_HEAD (frags);
  1211. BUG_ON(!state || !ldb);
  1212. size = ldb->vm.vblk_size;
  1213. perbuf = 512 / size;
  1214. skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */
  1215. finish = (size * ldb->vm.last_vblk_seq) >> 9;
  1216. for (s = skip; s < finish; s++) { /* For each sector */
  1217. data = read_part_sector(state, base + OFF_VMDB + s, &sect);
  1218. if (!data) {
  1219. ldm_crit ("Disk read failed.");
  1220. goto out;
  1221. }
  1222. for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */
  1223. if (MAGIC_VBLK != get_unaligned_be32(data)) {
  1224. ldm_error ("Expected to find a VBLK.");
  1225. goto out;
  1226. }
  1227. recs = get_unaligned_be16(data + 0x0E); /* Number of records */
  1228. if (recs == 1) {
  1229. if (!ldm_ldmdb_add (data, size, ldb))
  1230. goto out; /* Already logged */
  1231. } else if (recs > 1) {
  1232. if (!ldm_frag_add (data, size, &frags))
  1233. goto out; /* Already logged */
  1234. }
  1235. /* else Record is not in use, ignore it. */
  1236. }
  1237. put_dev_sector (sect);
  1238. data = NULL;
  1239. }
  1240. result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */
  1241. out:
  1242. if (data)
  1243. put_dev_sector (sect);
  1244. ldm_frag_free (&frags);
  1245. return result;
  1246. }
  1247. /**
  1248. * ldm_free_vblks - Free a linked list of vblk's
  1249. * @lh: Head of a linked list of struct vblk
  1250. *
  1251. * Free a list of vblk's and free the memory used to maintain the list.
  1252. *
  1253. * Return: none
  1254. */
  1255. static void ldm_free_vblks (struct list_head *lh)
  1256. {
  1257. struct list_head *item, *tmp;
  1258. BUG_ON (!lh);
  1259. list_for_each_safe (item, tmp, lh)
  1260. kfree (list_entry (item, struct vblk, list));
  1261. }
  1262. /**
  1263. * ldm_partition - Find out whether a device is a dynamic disk and handle it
  1264. * @state: Partition check state including device holding the LDM Database
  1265. *
  1266. * This determines whether the device @bdev is a dynamic disk and if so creates
  1267. * the partitions necessary in the gendisk structure pointed to by @hd.
  1268. *
  1269. * We create a dummy device 1, which contains the LDM database, and then create
  1270. * each partition described by the LDM database in sequence as devices 2+. For
  1271. * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
  1272. * and so on: the actual data containing partitions.
  1273. *
  1274. * Return: 1 Success, @state->disk is a dynamic disk and we handled it
  1275. * 0 Success, @state->disk is not a dynamic disk
  1276. * -1 An error occurred before enough information had been read
  1277. * Or @state->disk is a dynamic disk, but it may be corrupted
  1278. */
  1279. int ldm_partition(struct parsed_partitions *state)
  1280. {
  1281. struct ldmdb *ldb;
  1282. unsigned long base;
  1283. int result = -1;
  1284. BUG_ON(!state);
  1285. /* Look for signs of a Dynamic Disk */
  1286. if (!ldm_validate_partition_table(state))
  1287. return 0;
  1288. ldb = kmalloc (sizeof (*ldb), GFP_KERNEL);
  1289. if (!ldb) {
  1290. ldm_crit ("Out of memory.");
  1291. goto out;
  1292. }
  1293. /* Parse and check privheads. */
  1294. if (!ldm_validate_privheads(state, &ldb->ph))
  1295. goto out; /* Already logged */
  1296. /* All further references are relative to base (database start). */
  1297. base = ldb->ph.config_start;
  1298. /* Parse and check tocs and vmdb. */
  1299. if (!ldm_validate_tocblocks(state, base, ldb) ||
  1300. !ldm_validate_vmdb(state, base, ldb))
  1301. goto out; /* Already logged */
  1302. /* Initialize vblk lists in ldmdb struct */
  1303. INIT_LIST_HEAD (&ldb->v_dgrp);
  1304. INIT_LIST_HEAD (&ldb->v_disk);
  1305. INIT_LIST_HEAD (&ldb->v_volu);
  1306. INIT_LIST_HEAD (&ldb->v_comp);
  1307. INIT_LIST_HEAD (&ldb->v_part);
  1308. if (!ldm_get_vblks(state, base, ldb)) {
  1309. ldm_crit ("Failed to read the VBLKs from the database.");
  1310. goto cleanup;
  1311. }
  1312. /* Finally, create the data partition devices. */
  1313. if (ldm_create_data_partitions(state, ldb)) {
  1314. ldm_debug ("Parsed LDM database successfully.");
  1315. result = 1;
  1316. }
  1317. /* else Already logged */
  1318. cleanup:
  1319. ldm_free_vblks (&ldb->v_dgrp);
  1320. ldm_free_vblks (&ldb->v_disk);
  1321. ldm_free_vblks (&ldb->v_volu);
  1322. ldm_free_vblks (&ldb->v_comp);
  1323. ldm_free_vblks (&ldb->v_part);
  1324. out:
  1325. kfree (ldb);
  1326. return result;
  1327. }