xref: /illumos-gate/usr/src/cmd/hal/probing/volume/probe-volume.c (revision fe54a78e1aacf39261ad56e9903bce02e3fb6d21)
1 /***************************************************************************
2  *
3  * probe-volume.c : probe volumes
4  *
5  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
6  * Use is subject to license terms.
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  **************************************************************************/
11 
12 #ifdef HAVE_CONFIG_H
13 #  include <config.h>
14 #endif
15 
16 #include <errno.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <ctype.h>
26 #include <time.h>
27 #include <sys/time.h>
28 #include <sys/dkio.h>
29 #include <sys/cdio.h>
30 #include <sys/fdio.h>
31 #include <libnvpair.h>
32 #include <libfstyp.h>
33 #include <sys/vtoc.h>
34 #include <sys/efi_partition.h>
35 #include <sys/fs/hsfs_spec.h>
36 #include <sys/fs/hsfs_isospec.h>
37 #include <priv.h>
38 
39 #include <libhal.h>
40 #include <cdutils.h>
41 #include <fsutils.h>
42 #include <logger.h>
43 
44 static void
45 my_dbus_error_free(DBusError *error)
46 {
47 	if (dbus_error_is_set(error)) {
48 		dbus_error_free(error);
49 	}
50 }
51 
52 /*
53  * Return a copy of a string without trailing spaces. If 'len' is non-zero,
54  * it specifies max length, otherwise the string must be null-terminated.
55  */
56 static char *
57 rtrim_copy(char *src, int len)
58 {
59 	char	*dst, *p;
60 
61 	if (len == 0) {
62 		len = strlen(src);
63 	}
64 	if ((dst = calloc(1, len + 1)) != NULL) {
65 		strncpy(dst, src, len);
66 		p = dst + len - 1;
67 		while ((p >= dst) && (isspace(*p))) {
68 			*p-- = '\0';
69 		}
70 	}
71 	return (dst);
72 }
73 
74 static void
75 set_fstyp_properties (LibHalContext *ctx, const char *udi, const char *fstype, nvlist_t *fsattr)
76 {
77 	char buf[256];
78 	DBusError error;
79 	char *uuid = NULL;
80 	char *label_orig = NULL;
81 	char *label = NULL;
82 	LibHalChangeSet *cs;
83 
84 	dbus_error_init (&error);
85 
86 	if ((cs = libhal_device_new_changeset (udi)) == NULL) {
87 		return;
88 	}
89 
90 	libhal_changeset_set_property_string (cs, "volume.fsusage", "filesystem");
91 	libhal_changeset_set_property_string (cs, "volume.fstype", fstype);
92 
93 	/* label */
94 	(void) nvlist_lookup_string(fsattr, "gen_volume_label", &label_orig);
95 	if (label_orig != NULL) {
96 		label = rtrim_copy(label_orig, 0);
97 	}
98 	if ((label != NULL) && (label[0] != '\0')) {
99 		libhal_changeset_set_property_string (cs, "volume.label", label);
100 		libhal_changeset_set_property_string (cs, "info.product", label);
101 	} else {
102 		libhal_changeset_set_property_string (cs, "volume.label", "");
103 		snprintf (buf, sizeof (buf), "Volume (%s)", fstype);
104 		libhal_changeset_set_property_string (cs, "info.product", buf);
105 	}
106 	free(label);
107 
108 	/* uuid */
109 	if (nvlist_lookup_string(fsattr, "gen_uuid", &uuid) == 0) {
110 		libhal_changeset_set_property_string (cs, "volume.uuid", uuid);
111 	} else {
112 		libhal_changeset_set_property_string (cs, "volume.uuid", "");
113 	}
114 
115 	libhal_device_commit_changeset (ctx, cs, &error);
116 	libhal_device_free_changeset (cs);
117 
118 	my_dbus_error_free (&error);
119 }
120 
121 /*
122  * hsfs/iso9660 contents detection: Video DVD, Video CD, etc.
123  */
124 static void
125 hsfs_contents(int fd, off_t probe_offset, LibHalContext *ctx, const char *udi)
126 {
127 	size_t	secsz = ISO_SECTOR_SIZE;
128 	uchar_t	buf[ISO_SECTOR_SIZE];
129 	int	ptbl_lbn, ptbl_size;
130 	int	off, reloff, readoff;
131 	uchar_t	*p;
132 	char	*name;
133 	int	name_len;
134 	int	ipe_len;
135 	DBusError error;
136 
137 	/*
138 	 * find 1st Primary Volume Descriptor
139 	 */
140 	readoff = probe_offset + ISO_VOLDESC_SEC * secsz;
141 	if (pread (fd, buf, secsz, readoff) != secsz) {
142 		return;
143 	}
144 	while (ISO_DESC_TYPE (buf) != ISO_VD_PVD) {
145 		if (ISO_DESC_TYPE (buf) == ISO_VD_EOV) {
146 			return;
147 		}
148 		readoff += secsz;
149 		if (pread (fd, buf, secsz, readoff) != secsz) {
150 			return;
151 		}
152 	}
153 
154 	/*
155 	 * PVD contains size and offset of the LSB/MSB path table
156 	 */
157 	ptbl_size = ISO_PTBL_SIZE (buf);
158 #if defined(_LITTLE_ENDIAN)
159         ptbl_lbn = ISO_PTBL_MAN_LS (buf);
160 #else
161         ptbl_lbn = ISO_PTBL_MAN_MS (buf);
162 #endif
163 
164 	/*
165 	 * Look through path table entries
166 	 */
167 	readoff = probe_offset + ptbl_lbn * secsz;
168 	if (pread (fd, buf, secsz, readoff) != secsz) {
169 		return;
170 	}
171 	dbus_error_init (&error);
172 
173 	for (off = reloff = 0;
174 	    off < ptbl_size;
175 	    off += ipe_len, reloff += ipe_len) {
176 
177 		/* load sectors on demand */
178 		if (reloff >= secsz) {
179 			readoff += secsz;
180 			if (pread (fd, buf, secsz, readoff) != secsz) {
181 				break;
182 			}
183 			reloff -= secsz;
184 		}
185 
186 		p = buf + reloff;
187 		name_len = IPE_NAME_LEN(p);
188 		ipe_len = IPE_FPESIZE + name_len + (name_len % 2);
189 
190 		/* only interested in root directories */
191 		if (IPE_PARENT_NO (p) != 1) {
192 			continue;
193 		}
194 		if ((name_len < 2) || (name_len > IDE_MAX_NAME_LEN)) {
195 			continue;
196 		}
197 
198 		name = (char *)IPE_NAME (p);
199 		if (strncasecmp (name, "VIDEO_TS", min (8, name_len)) == 0) {
200 			libhal_device_set_property_bool (ctx, udi,
201 			    "volume.disc.is_videodvd", TRUE, &error);
202 		} else if (strncasecmp (name, "VCD", min (3, name_len)) == 0) {
203 			libhal_device_set_property_bool (ctx, udi,
204 			    "volume.disc.is_vcd", TRUE, &error);
205 		} else if (strncasecmp (name, "SVCD", min (4, name_len)) == 0) {
206 			libhal_device_set_property_bool (ctx, udi,
207 			    "volume.disc.is_svcd", TRUE, &error);
208 		}
209 	}
210 
211 	my_dbus_error_free (&error);
212 }
213 
214 static dbus_bool_t
215 probe_disc (int fd, LibHalContext *ctx, const char *udi, dbus_bool_t *has_data,
216     dbus_bool_t *has_audio)
217 {
218 	DBusError error;
219 	disc_info_t di;
220 	int profile;
221 	dbus_bool_t is_blank, is_appendable, is_rewritable;
222 	char *disc_type = "cd_rom";
223 	uint64_t capacity = 0;
224 	int i;
225 	LibHalChangeSet *cs;
226 
227 	dbus_error_init (&error);
228 
229 	if (get_disc_info (fd, &di)) {
230 		is_blank = (di.disc_status == 0);
231 		is_appendable = (di.disc_status == 1);
232 		is_rewritable = (di.erasable != 0);
233 	} else {
234 		is_blank = is_appendable = is_rewritable = FALSE;
235 	}
236 
237 	if (get_current_profile (fd, &profile)) {
238 		switch (profile) {
239 		case 0x08: /* CD-ROM */
240 			disc_type = "cd_rom";
241 			break;
242 		case 0x09: /* CD-R */
243 			disc_type = "cd_r";
244 			break;
245 		case 0x0A: /* CD-RW */
246 			disc_type = "cd_rw";
247 			is_rewritable = TRUE;
248 			break;
249 		case 0x10: /* DVD-ROM */
250 			disc_type = "dvd_rom";
251 			break;
252 		case 0x11: /* DVD-R Sequential */
253 			disc_type = "dvd_r";
254 			break;
255 		case 0x12: /* DVD-RAM */
256 			disc_type = "dvd_ram";
257 			is_rewritable = TRUE;
258 			break;
259 		case 0x13: /* DVD-RW Restricted Overwrite */
260 			disc_type = "dvd_rw";
261 			is_rewritable = TRUE;
262 			break;
263 		case 0x14: /* DVD-RW Sequential */
264 			disc_type = "dvd_rw";
265 			is_rewritable = TRUE;
266 			break;
267 		case 0x1A: /* DVD+RW */
268 			disc_type = "dvd_plus_rw";
269 			is_rewritable = TRUE;
270 			break;
271 		case 0x1B: /* DVD+R */
272 			disc_type = "dvd_plus_r";
273 			break;
274 		case 0x2B: /* DVD+R Double Layer */
275                         disc_type = "dvd_plus_r_dl";
276 			break;
277 		case 0x40: /* BD-ROM */
278                         disc_type = "bd_rom";
279 			break;
280 		case 0x41: /* BD-R Sequential */
281                         disc_type = "bd_r";
282 			break;
283 		case 0x42: /* BD-R Random */
284                         disc_type = "bd_r";
285 			break;
286 		case 0x43: /* BD-RE */
287                         disc_type = "bd_re";
288 			is_rewritable = TRUE;
289 			break;
290 		case 0x50: /* HD DVD-ROM */
291                         disc_type = "hddvd_rom";
292 			break;
293 		case 0x51: /* HD DVD-R */
294                         disc_type = "hddvd_r";
295 			break;
296 		case 0x52: /* HD DVD-Rewritable */
297                         disc_type = "hddvd_rw";
298 			is_rewritable = TRUE;
299 			break;
300 		}
301 
302 		(void) get_disc_capacity_for_profile(fd, profile, &capacity);
303 	}
304 
305 	*has_audio = *has_data = FALSE;
306 	if (!is_blank) {
307 		uchar_t	smalltoc[12];
308 		size_t	toc_size;
309 		uchar_t	*toc, *p;
310 
311 		/*
312 		 * XXX for some reason CDROMREADTOCENTRY fails on video DVDs,
313 		 * but extracting the toc directly works okay.
314 		 */
315         	if (!read_toc(fd, 0, 1, 4, smalltoc)) {
316                 	HAL_DEBUG(("read_toc failed"));
317 			*has_data = B_TRUE; /* probe for fs anyway */
318         	} else {
319         		toc_size = smalltoc[0] * 256 + smalltoc[1] + 2;
320         		toc = (uchar_t *)calloc(1, toc_size);
321         		if (toc == NULL || !read_toc(fd, 0, 1, toc_size, toc)) {
322                 		HAL_DEBUG (("read_toc again failed"));
323         		} else {
324         			for (p = &toc[4]; p < (toc + toc_size); p += 8) {
325 					/* skip leadout */
326                 			if (p[2] == 0xAA) {
327 						continue;
328 					}
329 					if (p[1] & 4) {
330 						*has_data = B_TRUE;
331 					} else {
332 						*has_audio = B_TRUE;
333 					}
334         			}
335 			}
336 			free(toc);
337 		}
338 	}
339 
340 	if ((cs = libhal_device_new_changeset (udi)) == NULL) {
341 		return (FALSE);
342 	}
343 	libhal_changeset_set_property_string (cs, "volume.disc.type", disc_type);
344 	libhal_changeset_set_property_bool (cs, "volume.disc.is_blank", is_blank);
345 	libhal_changeset_set_property_bool (cs, "volume.disc.has_audio", *has_audio);
346 	libhal_changeset_set_property_bool (cs, "volume.disc.has_data", *has_data);
347 	libhal_changeset_set_property_bool (cs, "volume.disc.is_appendable", is_appendable);
348 	libhal_changeset_set_property_bool (cs, "volume.disc.is_rewritable", is_rewritable);
349 	libhal_changeset_set_property_uint64 (cs, "volume.disc.capacity", capacity);
350 
351 	libhal_changeset_set_property_bool (cs, "volume.disc.is_videodvd", FALSE);
352 	libhal_changeset_set_property_bool (cs, "volume.disc.is_vcd", FALSE);
353 	libhal_changeset_set_property_bool (cs, "volume.disc.is_svcd", FALSE);
354 
355 	libhal_device_commit_changeset (ctx, cs, &error);
356 	libhal_device_free_changeset (cs);
357 
358 out:
359 	my_dbus_error_free (&error);
360 
361 	return (TRUE);
362 }
363 
364 static void
365 drop_privileges ()
366 {
367 	priv_set_t *pPrivSet = NULL;
368 	priv_set_t *lPrivSet = NULL;
369 
370 	/*
371 	 * Start with the 'basic' privilege set and then remove any
372 	 * of the 'basic' privileges that will not be needed.
373 	 */
374 	if ((pPrivSet = priv_str_to_set("basic", ",", NULL)) == NULL) {
375 		return;
376 	}
377 
378 	/* Clear privileges we will not need from the 'basic' set */
379 	(void) priv_delset(pPrivSet, PRIV_FILE_LINK_ANY);
380 	(void) priv_delset(pPrivSet, PRIV_PROC_INFO);
381 	(void) priv_delset(pPrivSet, PRIV_PROC_SESSION);
382 	(void) priv_delset(pPrivSet, PRIV_PROC_EXEC);
383 	(void) priv_delset(pPrivSet, PRIV_PROC_FORK);
384 
385 	/* for uscsi */
386 	(void) priv_addset(pPrivSet, PRIV_SYS_DEVICES);
387 
388 
389 	/* to open logindevperm'd devices */
390 	(void) priv_addset(pPrivSet, PRIV_FILE_DAC_READ);
391 
392 	/* Set the permitted privilege set. */
393 	if (setppriv(PRIV_SET, PRIV_PERMITTED, pPrivSet) != 0) {
394 		return;
395 	}
396 
397 	/* Clear the limit set. */
398 	if ((lPrivSet = priv_allocset()) == NULL) {
399 		return;
400 	}
401 
402 	priv_emptyset(lPrivSet);
403 
404 	if (setppriv(PRIV_SET, PRIV_LIMIT, lPrivSet) != 0) {
405 		return;
406 	}
407 
408 	priv_freeset(lPrivSet);
409 }
410 
411 int
412 main (int argc, char *argv[])
413 {
414 	int fd, rfd;
415 	int ret;
416 	char *udi;
417 	char *device_file, *raw_device_file;
418 	char *devpath, *rdevpath;
419 	boolean_t is_dos;
420 	int dos_num;
421 	LibHalContext *ctx = NULL;
422 	DBusError error;
423 	DBusConnection *conn;
424 	char *parent_udi;
425 	char *storage_device;
426 	char *is_disc_str;
427 	int fdc;
428 	dbus_bool_t is_disc = FALSE;
429 	dbus_bool_t is_floppy = FALSE;
430 	unsigned int block_size;
431 	dbus_uint64_t vol_size;
432 	dbus_bool_t has_data = TRUE;	/* probe for fs by default */
433 	dbus_bool_t has_audio = FALSE;
434 	char *partition_scheme = NULL;
435 	dbus_uint64_t partition_start = 0;
436 	int partition_number = 0;
437 	struct extvtoc vtoc;
438 	dk_gpt_t *gpt;
439 	struct dk_minfo mi;
440 	int i, dos_cnt;
441 	fstyp_handle_t fstyp_handle;
442 	off_t probe_offset = 0;
443 	int num_volumes;
444 	char **volumes;
445 	dbus_uint64_t v_start;
446 	const char *fstype;
447 	nvlist_t *fsattr;
448 
449 	fd = rfd = -1;
450 
451 	ret = 1;
452 
453 	if ((udi = getenv ("UDI")) == NULL) {
454 		goto out;
455 	}
456 	if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL) {
457 		goto out;
458 	}
459 	if ((raw_device_file = getenv ("HAL_PROP_BLOCK_SOLARIS_RAW_DEVICE")) == NULL) {
460 		goto out;
461 	}
462 	if (!dos_to_dev(raw_device_file, &rdevpath, &dos_num)) {
463 		rdevpath = raw_device_file;
464 	}
465 	if (!(is_dos = dos_to_dev(device_file, &devpath, &dos_num))) {
466 		devpath = device_file;
467 	}
468 	if ((parent_udi = getenv ("HAL_PROP_INFO_PARENT")) == NULL) {
469 		goto out;
470 	}
471 	if ((storage_device = getenv ("HAL_PROP_BLOCK_STORAGE_DEVICE")) == NULL) {
472 		goto out;
473 	}
474 
475 	is_disc_str = getenv ("HAL_PROP_VOLUME_IS_DISC");
476 	if (is_disc_str != NULL && strcmp (is_disc_str, "true") == 0) {
477 		is_disc = TRUE;
478 	} else {
479 		is_disc = FALSE;
480 	}
481 
482 	drop_privileges ();
483 
484 	setup_logger ();
485 
486 	dbus_error_init (&error);
487 	if ((ctx = libhal_ctx_init_direct (&error)) == NULL)
488 		goto out;
489 
490 	HAL_DEBUG (("Doing probe-volume for %s\n", device_file));
491 
492 	fd = open (devpath, O_RDONLY | O_NONBLOCK);
493 	if (fd < 0) {
494 		goto out;
495 	}
496 	rfd = open (rdevpath, O_RDONLY | O_NONBLOCK);
497 	if (rfd < 0) {
498 		goto out;
499 	}
500 
501 	/* if it's a floppy with no media, bail out */
502 	if (ioctl(rfd, FDGETCHANGE, &fdc) == 0) {
503 		is_floppy = TRUE;
504 		if (fdc & FDGC_CURRENT) {
505 			goto out;
506 		}
507 	}
508 
509 	/* block size and total size */
510 	if (ioctl(rfd, DKIOCGMEDIAINFO, &mi) != -1) {
511 		block_size = mi.dki_lbsize;
512 		vol_size = mi.dki_capacity * block_size;
513 	} else if (errno == ENXIO) {
514 		/* driver supports ioctl, but media is not available */
515 		goto out;
516 	} else {
517 		/* driver does not support ioctl, e.g. lofi */
518 		block_size = 512;
519 		vol_size = 0;
520 	}
521 	libhal_device_set_property_int (ctx, udi, "volume.block_size", block_size, &error);
522 	my_dbus_error_free (&error);
523 	libhal_device_set_property_uint64 (ctx, udi, "volume.size", vol_size, &error);
524 	my_dbus_error_free (&error);
525 
526 	if (is_disc) {
527 		if (!probe_disc (rfd, ctx, udi, &has_data, &has_audio)) {
528 			HAL_DEBUG (("probe_disc failed, skipping fstyp"));
529 			goto out;
530 		}
531 		/* with audio present, create volume even if fs probing fails */
532 		if (has_audio) {
533 			ret = 0;
534 		}
535 	}
536 
537 	if (!has_data) {
538 		goto skip_fs;
539 	}
540 
541 	/* don't support partitioned floppy */
542 	if (is_floppy) {
543 		goto skip_part;
544 	}
545 
546 	/*
547 	 * first get partitioning info
548 	 */
549 	if (is_dos) {
550 		/* for a dos drive find partition offset */
551 		if (!find_dos_drive(fd, dos_num, block_size, &probe_offset)) {
552 			goto out;
553 		}
554 		partition_scheme = "mbr";
555 		partition_start = (dbus_uint64_t)probe_offset;
556 		partition_number = dos_num;
557 	} else {
558 		if ((partition_number = read_extvtoc(rfd, &vtoc)) >= 0) {
559 			if (!vtoc_one_slice_entire_disk(&vtoc)) {
560 				partition_scheme = "smi";
561 				if (partition_number < vtoc.v_nparts) {
562 					if (vtoc.v_part[partition_number].p_size == 0) {
563 						HAL_DEBUG (("zero size partition"));
564 					}
565 					partition_start = vtoc.v_part[partition_number].p_start * block_size;
566 				}
567 			}
568 		} else if ((partition_number = efi_alloc_and_read(rfd, &gpt)) >= 0) {
569 			partition_scheme = "gpt";
570 			if (partition_number < gpt->efi_nparts) {
571 				if (gpt->efi_parts[partition_number].p_size == 0) {
572 					HAL_DEBUG (("zero size partition"));
573 				}
574 				partition_start = gpt->efi_parts[partition_number].p_start * block_size;
575 			}
576 			efi_free(gpt);
577 		}
578 		probe_offset = 0;
579 	}
580 
581 	if (partition_scheme != NULL) {
582 		libhal_device_set_property_string (ctx, udi, "volume.partition.scheme", partition_scheme, &error);
583 		my_dbus_error_free (&error);
584 		libhal_device_set_property_int (ctx, udi, "volume.partition.number", partition_number, &error);
585 		my_dbus_error_free (&error);
586 		libhal_device_set_property_uint64 (ctx, udi, "volume.partition.start", partition_start, &error);
587 		my_dbus_error_free (&error);
588 		libhal_device_set_property_bool (ctx, udi, "volume.is_partition", TRUE, &error);
589 		my_dbus_error_free (&error);
590 	} else {
591 		libhal_device_set_property_bool (ctx, udi, "volume.is_partition", FALSE, &error);
592 		my_dbus_error_free (&error);
593 	}
594 
595 	/*
596 	 * ignore duplicate partitions
597 	 */
598 	if ((volumes = libhal_manager_find_device_string_match (
599 	    ctx, "block.storage_device", storage_device, &num_volumes, &error)) != NULL) {
600 		my_dbus_error_free (&error);
601 		for (i = 0; i < num_volumes; i++) {
602 			if (strcmp (udi, volumes[i]) == 0) {
603 				continue; /* skip self */
604 			}
605 			v_start = libhal_device_get_property_uint64 (ctx, volumes[i], "volume.partition.start", &error);
606 			if (dbus_error_is_set(&error)) {
607 				dbus_error_free(&error);
608 				continue;
609 			}
610 			if (v_start == partition_start) {
611 				HAL_DEBUG (("duplicate partition"));
612 				goto out;
613 			}
614 		}
615 		libhal_free_string_array (volumes);
616 	}
617 
618 skip_part:
619 
620 	/*
621 	 * now determine fs type
622 	 *
623 	 * XXX We could get better performance from block device,
624 	 * but for now we use raw device because:
625 	 *
626 	 * - fstyp_udfs has a bug that it only works on raw
627 	 *
628 	 * - sd has a bug that causes extremely slow reads
629 	 *   and incorrect probing of hybrid audio/data media
630 	 */
631 	if (fstyp_init(rfd, probe_offset, NULL, &fstyp_handle) != 0) {
632 		HAL_DEBUG (("fstyp_init failed"));
633 		goto out;
634 	}
635 	if ((fstyp_ident(fstyp_handle, NULL, &fstype) != 0) ||
636 	    (fstyp_get_attr(fstyp_handle, &fsattr) != 0)) {
637 		HAL_DEBUG (("fstyp ident or get_attr failed"));
638 		fstyp_fini(fstyp_handle);
639 		goto out;
640 	}
641 	set_fstyp_properties (ctx, udi, fstype, fsattr);
642 
643 	if (strcmp (fstype, "hsfs") == 0) {
644 		hsfs_contents (fd, probe_offset, ctx, udi);
645 	}
646 
647 	fstyp_fini(fstyp_handle);
648 
649 skip_fs:
650 
651 	ret = 0;
652 
653 out:
654 	if (fd >= 0)
655 		close (fd);
656 	if (rfd >= 0)
657 		close (rfd);
658 
659 	if (ctx != NULL) {
660 		my_dbus_error_free (&error);
661 		libhal_ctx_shutdown (ctx, &error);
662 		libhal_ctx_free (ctx);
663 	}
664 
665 	return ret;
666 
667 }
668