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