summaryrefslogtreecommitdiff
path: root/vfat-volid/vfat-volid.c
diff options
context:
space:
mode:
Diffstat (limited to 'vfat-volid/vfat-volid.c')
-rw-r--r--vfat-volid/vfat-volid.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/vfat-volid/vfat-volid.c b/vfat-volid/vfat-volid.c
new file mode 100644
index 0000000..91cb09b
--- /dev/null
+++ b/vfat-volid/vfat-volid.c
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Linaro
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Linaro <linaro-dev@lists.linaro.org>
+ *******************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/msdos_fs.h>
+
+#ifndef VFAT_IOCTL_GET_VOLUME_ID
+#warning The IOCTL code has not been defined in public headers...
+#define VFAT_IOCTL_GET_VOLUME_ID _IOR('r', 0x12, __u32)
+#endif
+
+#ifndef VFAT_IOCTL_GET_VOLUME_ID_SAFE
+#warning The IOCTL safe code has not been defined in public headers...
+#define VFAT_IOCTL_GET_VOLUME_ID_SAFE _IOR('r', 0x13, __u32)
+#endif
+
+int main(int argc, char *argv[])
+{
+ struct option lopt[] = {
+ { "path", required_argument, NULL, 'p' },
+ { "ignore-error", no_argument, NULL, 'i' },
+ { "use-safe-ioctl", no_argument, NULL, 's' },
+ { "use-bad-addr", no_argument, NULL, 'e' },
+ { NULL, 0, NULL, 0 },
+ };
+ char * shopt = "p:ise";
+ char *device = NULL;
+ long res;
+ int h;
+ int c, opti,
+ ignore_ioctl_error = 0,
+ use_bad_addr = 0,
+ use_safe_ioctl = 0;
+ __u32 id;
+
+ while ((c = getopt_long(argc, argv, shopt, lopt, &opti)) != -1 ) {
+ switch (c) {
+ case 'p':
+ device = strdup(optarg);
+ break;
+ case 'i':
+ ignore_ioctl_error = 1;
+ break;
+ case 's':
+ use_safe_ioctl = 1;
+ break;
+ case 'e':
+ use_bad_addr = 1;
+ break;
+ default:
+ printf("Warning, unknown option!\n");
+ break;
+ }
+ }
+
+ if (!device) {
+ printf("Missing parameter, please use --path=<mounted fat partition> option\n");
+ return ENODEV;
+ }
+
+ h = open(device, O_RDONLY);
+ if (h < 0) {
+ perror("Error on open");
+ free(device);
+ return -h;
+ }
+
+ if (use_safe_ioctl) {
+ res = ioctl(h, VFAT_IOCTL_GET_VOLUME_ID_SAFE, use_bad_addr ? 0x1234 : &id);
+ } else {
+ res = ioctl(h, VFAT_IOCTL_GET_VOLUME_ID);
+ id = res;
+ }
+ if (res < 0 && !ignore_ioctl_error) {
+ perror("Error on ioctl");
+ free(device);
+ return -res;
+ }
+ printf("0x%08X", id);
+
+ free(device);
+ return 0;
+}