xref: /linux/block/partitions/ibm.c (revision f990ad67f0febc51274adb604d5bdeab0d06d024)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Volker Sameske <sameske@de.ibm.com>
5  * Bugreports.to..: <Linux390@de.ibm.com>
6  * Copyright IBM Corp. 1999, 2012
7  */
8 
9 #include <linux/buffer_head.h>
10 #include <linux/hdreg.h>
11 #include <linux/slab.h>
12 #include <asm/dasd.h>
13 #include <asm/ebcdic.h>
14 #include <linux/uaccess.h>
15 #include <asm/vtoc.h>
16 #include <linux/module.h>
17 #include <linux/dasd_mod.h>
18 
19 #include "check.h"
20 
21 union label_t {
22 	struct vtoc_volume_label_cdl vol;
23 	struct vtoc_volume_label_ldl lnx;
24 	struct vtoc_cms_label cms;
25 };
26 
27 /*
28  * compute the block number from a
29  * cyl-cyl-head-head structure
30  */
31 static sector_t cchh2blk(struct vtoc_cchh *ptr, struct hd_geometry *geo)
32 {
33 	sector_t cyl;
34 	__u16 head;
35 
36 	/* decode cylinder and heads for large volumes */
37 	cyl = ptr->hh & 0xFFF0;
38 	cyl <<= 12;
39 	cyl |= ptr->cc;
40 	head = ptr->hh & 0x000F;
41 	return cyl * geo->heads * geo->sectors +
42 	       head * geo->sectors;
43 }
44 
45 /*
46  * compute the block number from a
47  * cyl-cyl-head-head-block structure
48  */
49 static sector_t cchhb2blk(struct vtoc_cchhb *ptr, struct hd_geometry *geo)
50 {
51 	sector_t cyl;
52 	__u16 head;
53 
54 	/* decode cylinder and heads for large volumes */
55 	cyl = ptr->hh & 0xFFF0;
56 	cyl <<= 12;
57 	cyl |= ptr->cc;
58 	head = ptr->hh & 0x000F;
59 	return	cyl * geo->heads * geo->sectors +
60 		head * geo->sectors +
61 		ptr->b;
62 }
63 
64 /* Volume Label Type/ID Length */
65 #define DASD_VOL_TYPE_LEN	4
66 #define DASD_VOL_ID_LEN		6
67 
68 /* Volume Label Types */
69 #define DASD_VOLLBL_TYPE_VOL1 0
70 #define DASD_VOLLBL_TYPE_LNX1 1
71 #define DASD_VOLLBL_TYPE_CMS1 2
72 
73 struct dasd_vollabel {
74 	char *type;
75 	int idx;
76 };
77 
78 static struct dasd_vollabel dasd_vollabels[] = {
79 	[DASD_VOLLBL_TYPE_VOL1] = {
80 		.type = "VOL1",
81 		.idx = DASD_VOLLBL_TYPE_VOL1,
82 	},
83 	[DASD_VOLLBL_TYPE_LNX1] = {
84 		.type = "LNX1",
85 		.idx = DASD_VOLLBL_TYPE_LNX1,
86 	},
87 	[DASD_VOLLBL_TYPE_CMS1] = {
88 		.type = "CMS1",
89 		.idx = DASD_VOLLBL_TYPE_CMS1,
90 	},
91 };
92 
93 static int get_label_by_type(const char *type)
94 {
95 	int i;
96 
97 	for (i = 0; i < ARRAY_SIZE(dasd_vollabels); i++) {
98 		if (!memcmp(type, dasd_vollabels[i].type, DASD_VOL_TYPE_LEN))
99 			return dasd_vollabels[i].idx;
100 	}
101 
102 	return -1;
103 }
104 
105 static int find_label(struct parsed_partitions *state,
106 		      dasd_information2_t *info,
107 		      struct hd_geometry *geo,
108 		      int blocksize,
109 		      sector_t *labelsect,
110 		      char name[],
111 		      char type[],
112 		      union label_t *label)
113 {
114 	sector_t testsect[3];
115 	int i, testcount;
116 	Sector sect;
117 	void *data;
118 
119 	/* There a three places where we may find a valid label:
120 	 * - on an ECKD disk it's block 2
121 	 * - on an FBA disk it's block 1
122 	 * - on an CMS formatted FBA disk it is sector 1, even if the block size
123 	 *   is larger than 512 bytes (possible if the DIAG discipline is used)
124 	 * If we have a valid info structure, then we know exactly which case we
125 	 * have, otherwise we just search through all possebilities.
126 	 */
127 	if (info) {
128 		if ((info->cu_type == 0x6310 && info->dev_type == 0x9336) ||
129 		    (info->cu_type == 0x3880 && info->dev_type == 0x3370))
130 			testsect[0] = info->label_block;
131 		else
132 			testsect[0] = info->label_block * (blocksize >> 9);
133 		testcount = 1;
134 	} else {
135 		testsect[0] = 1;
136 		testsect[1] = (blocksize >> 9);
137 		testsect[2] = 2 * (blocksize >> 9);
138 		testcount = 3;
139 	}
140 	for (i = 0; i < testcount; ++i) {
141 		data = read_part_sector(state, testsect[i], &sect);
142 		if (data == NULL)
143 			continue;
144 		memcpy(label, data, sizeof(*label));
145 		memcpy(type, data, DASD_VOL_TYPE_LEN);
146 		EBCASC(type, DASD_VOL_TYPE_LEN);
147 		put_dev_sector(sect);
148 		switch (get_label_by_type(type)) {
149 		case DASD_VOLLBL_TYPE_VOL1:
150 			memcpy(name, label->vol.volid, DASD_VOL_ID_LEN);
151 			EBCASC(name, DASD_VOL_ID_LEN);
152 			*labelsect = testsect[i];
153 			return 1;
154 		case DASD_VOLLBL_TYPE_LNX1:
155 		case DASD_VOLLBL_TYPE_CMS1:
156 			memcpy(name, label->lnx.volid, DASD_VOL_ID_LEN);
157 			EBCASC(name, DASD_VOL_ID_LEN);
158 			*labelsect = testsect[i];
159 			return 1;
160 		default:
161 			break;
162 		}
163 	}
164 
165 	return 0;
166 }
167 
168 static int find_vol1_partitions(struct parsed_partitions *state,
169 				struct hd_geometry *geo,
170 				int blocksize,
171 				char name[],
172 				union label_t *label)
173 {
174 	sector_t blk;
175 	int counter;
176 	Sector sect;
177 	unsigned char *data;
178 	loff_t offset, size;
179 	struct vtoc_format1_label f1;
180 	int secperblk;
181 
182 	seq_buf_printf(&state->pp_buf, "VOL1/%8s:", name);
183 	/*
184 	 * get start of VTOC from the disk label and then search for format1
185 	 * and format8 labels
186 	 */
187 	secperblk = blocksize >> 9;
188 	blk = cchhb2blk(&label->vol.vtoc, geo) + 1;
189 	counter = 0;
190 	data = read_part_sector(state, blk * secperblk, &sect);
191 	while (data != NULL) {
192 		memcpy(&f1, data, sizeof(struct vtoc_format1_label));
193 		put_dev_sector(sect);
194 		/* skip FMT4 / FMT5 / FMT7 labels */
195 		if (f1.DS1FMTID == _ascebc['4']
196 		    || f1.DS1FMTID == _ascebc['5']
197 		    || f1.DS1FMTID == _ascebc['7']
198 		    || f1.DS1FMTID == _ascebc['9']) {
199 			blk++;
200 			data = read_part_sector(state, blk * secperblk, &sect);
201 			continue;
202 		}
203 		/* only FMT1 and 8 labels valid at this point */
204 		if (f1.DS1FMTID != _ascebc['1'] &&
205 		    f1.DS1FMTID != _ascebc['8'])
206 			break;
207 		/* OK, we got valid partition data */
208 		offset = cchh2blk(&f1.DS1EXT1.llimit, geo);
209 		size  = cchh2blk(&f1.DS1EXT1.ulimit, geo) -
210 			offset + geo->sectors;
211 		offset *= secperblk;
212 		size *= secperblk;
213 		if (counter >= state->limit)
214 			break;
215 		put_partition(state, counter + 1, offset, size);
216 		counter++;
217 		blk++;
218 		data = read_part_sector(state, blk * secperblk, &sect);
219 	}
220 	seq_buf_puts(&state->pp_buf, "\n");
221 
222 	if (!data)
223 		return -1;
224 
225 	return 1;
226 }
227 
228 static int find_lnx1_partitions(struct parsed_partitions *state,
229 				struct hd_geometry *geo,
230 				int blocksize,
231 				char name[],
232 				union label_t *label,
233 				sector_t labelsect,
234 				sector_t nr_sectors,
235 				dasd_information2_t *info)
236 {
237 	loff_t offset, geo_size, size;
238 	int secperblk;
239 
240 	seq_buf_printf(&state->pp_buf, "LNX1/%8s:", name);
241 	secperblk = blocksize >> 9;
242 	if (label->lnx.ldl_version == 0xf2) {
243 		size = label->lnx.formatted_blocks * secperblk;
244 	} else {
245 		/*
246 		 * Formated w/o large volume support. If the sanity check
247 		 * 'size based on geo == size based on nr_sectors' is true, then
248 		 * we can safely assume that we know the formatted size of
249 		 * the disk, otherwise we need additional information
250 		 * that we can only get from a real DASD device.
251 		 */
252 		geo_size = geo->cylinders * geo->heads
253 			* geo->sectors * secperblk;
254 		size = nr_sectors;
255 		if (size != geo_size) {
256 			if (!info) {
257 				seq_buf_puts(&state->pp_buf, "\n");
258 				return 1;
259 			}
260 			if (!strcmp(info->type, "ECKD"))
261 				if (geo_size < size)
262 					size = geo_size;
263 			/* else keep size based on nr_sectors */
264 		}
265 	}
266 	/* first and only partition starts in the first block after the label */
267 	offset = labelsect + secperblk;
268 	put_partition(state, 1, offset, size - offset);
269 	seq_buf_puts(&state->pp_buf, "\n");
270 	return 1;
271 }
272 
273 static int find_cms1_partitions(struct parsed_partitions *state,
274 				struct hd_geometry *geo,
275 				int blocksize,
276 				char name[],
277 				union label_t *label,
278 				sector_t labelsect)
279 {
280 	loff_t offset, size;
281 	int secperblk;
282 
283 	/*
284 	 * VM style CMS1 labeled disk
285 	 */
286 	blocksize = label->cms.block_size;
287 	secperblk = blocksize >> 9;
288 	if (label->cms.disk_offset != 0) {
289 		seq_buf_printf(&state->pp_buf, "CMS1/%8s(MDSK):", name);
290 		/* disk is reserved minidisk */
291 		offset = label->cms.disk_offset * secperblk;
292 		size = (label->cms.block_count - 1) * secperblk;
293 	} else {
294 		seq_buf_printf(&state->pp_buf, "CMS1/%8s:", name);
295 		/*
296 		 * Special case for FBA devices:
297 		 * If an FBA device is CMS formatted with blocksize > 512 byte
298 		 * and the DIAG discipline is used, then the CMS label is found
299 		 * in sector 1 instead of block 1. However, the partition is
300 		 * still supposed to start in block 2.
301 		 */
302 		if (labelsect == 1)
303 			offset = 2 * secperblk;
304 		else
305 			offset = labelsect + secperblk;
306 		size = label->cms.block_count * secperblk;
307 	}
308 
309 	put_partition(state, 1, offset, size-offset);
310 	seq_buf_puts(&state->pp_buf, "\n");
311 	return 1;
312 }
313 
314 
315 /*
316  * This is the main function, called by check.c
317  */
318 int ibm_partition(struct parsed_partitions *state)
319 {
320 	int (*fn)(struct gendisk *disk, dasd_information2_t *info);
321 	struct gendisk *disk = state->disk;
322 	struct block_device *bdev = disk->part0;
323 	int blocksize, res;
324 	loff_t offset, size;
325 	sector_t nr_sectors;
326 	dasd_information2_t *info;
327 	struct hd_geometry *geo;
328 	char type[DASD_VOL_TYPE_LEN + 1] = "";
329 	char name[DASD_VOL_ID_LEN + 1] = "";
330 	sector_t labelsect;
331 	union label_t *label;
332 
333 	res = 0;
334 	if (!disk->fops->getgeo)
335 		goto out_exit;
336 	fn = symbol_get(dasd_biodasdinfo);
337 	blocksize = bdev_logical_block_size(bdev);
338 	if (blocksize <= 0)
339 		goto out_symbol;
340 	nr_sectors = bdev_nr_sectors(bdev);
341 	if (nr_sectors == 0)
342 		goto out_symbol;
343 	info = kmalloc_obj(dasd_information2_t);
344 	if (info == NULL)
345 		goto out_symbol;
346 	geo = kmalloc_obj(struct hd_geometry);
347 	if (geo == NULL)
348 		goto out_nogeo;
349 	label = kmalloc_obj(union label_t);
350 	if (label == NULL)
351 		goto out_nolab;
352 	/* set start if not filled by getgeo function e.g. virtblk */
353 	geo->start = get_start_sect(bdev);
354 	if (disk->fops->getgeo(disk, geo))
355 		goto out_freeall;
356 	if (!fn || fn(disk, info)) {
357 		kfree(info);
358 		info = NULL;
359 	}
360 
361 	if (find_label(state, info, geo, blocksize, &labelsect, name, type, label)) {
362 		switch (get_label_by_type(type)) {
363 		case DASD_VOLLBL_TYPE_VOL1:
364 			res = find_vol1_partitions(state, geo, blocksize, name,
365 						   label);
366 			break;
367 		case DASD_VOLLBL_TYPE_LNX1:
368 			res = find_lnx1_partitions(state, geo, blocksize, name,
369 						   label, labelsect, nr_sectors,
370 						   info);
371 			break;
372 		case DASD_VOLLBL_TYPE_CMS1:
373 			res = find_cms1_partitions(state, geo, blocksize, name,
374 						   label, labelsect);
375 			break;
376 		}
377 	} else if (info) {
378 		/*
379 		 * ugly but needed for backward compatibility:
380 		 * If the block device is a DASD (i.e. BIODASDINFO2 works),
381 		 * then we claim it in any case, even though it has no valid
382 		 * label. If it has the LDL format, then we simply define a
383 		 * partition as if it had an LNX1 label.
384 		 */
385 		res = 1;
386 		if (info->format == DASD_FORMAT_LDL) {
387 			seq_buf_puts(&state->pp_buf, "(nonl)");
388 			size = nr_sectors;
389 			offset = (info->label_block + 1) * (blocksize >> 9);
390 			put_partition(state, 1, offset, size-offset);
391 			seq_buf_puts(&state->pp_buf, "\n");
392 		}
393 	} else
394 		res = 0;
395 
396 out_freeall:
397 	kfree(label);
398 out_nolab:
399 	kfree(geo);
400 out_nogeo:
401 	kfree(info);
402 out_symbol:
403 	if (fn)
404 		symbol_put(dasd_biodasdinfo);
405 out_exit:
406 	return res;
407 }
408