return 0;
}
-static void mount_dev(const char *dev, const char *fstype)
+static char *get_partition_fstype(const char *devname)
{
+ blkid_tag_iterate tag_iter;
+ blkid_dev dev;
+ blkid_cache cache = NULL;
+ const char *type, *value;
int ret;
+ ret = blkid_get_cache(&cache, NULL);
+ if (ret < 0)
+ return NULL;
+
+ blkid_probe_all(cache);
+
+ dev = blkid_get_dev(cache, devname, 0);
+ if (!dev)
+ return NULL;
+
+ dev = blkid_verify(cache, dev);
+ if (!dev)
+ return NULL;
+
+ tag_iter = blkid_tag_iterate_begin(dev);
+ while (blkid_tag_next(tag_iter, &type, &value) == 0) {
+ if (!strncmp(type, "TYPE", 4)) {
+ char *fstype = strdup(value);
+
+ if (!fstype)
+ return NULL;
+
+ blkid_tag_iterate_end(tag_iter);
+ blkid_put_cache(cache);
+ return fstype;
+ }
+ }
+ blkid_tag_iterate_end(tag_iter);
+ blkid_put_cache(cache);
+
+ return NULL;
+}
+
+static int mount_dev(const char *dev)
+{
+ int ret;
+ char *fstype;
+
ret = mkdir(DFU_MOUNT_PATH, 0600);
if (ret < 0) {
fprintf(stderr, "Failed to create target directory\n");
- exit(-1);
+ return ret;
+ }
+
+ fstype = get_partition_fstype(dev);
+ if (!fstype) {
+ fprintf(stderr, "failed to get partition filesystem type: %s\n", dev);
+ return -EINVAL;
}
ret = mount(dev, DFU_MOUNT_PATH, fstype, 0, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to mount target partition\n");
rmdir(DFU_MOUNT_PATH);
- exit(-1);
}
+
+ free(fstype);
+
+ return ret;
}
static void umount_dev(void)
int ret;
ret = umount(DFU_MOUNT_PATH);
- if (ret < 0) {
- fprintf(stderr, "Failed to mount target partition\n");
-
- /* umount is failed, but anyway try to delete mount point */
- rmdir(DFU_MOUNT_PATH);
- exit(-1);
- }
+ if (ret < 0)
+ fprintf(stderr, "Failed to unmount target partition\n");
+ /* whether umount is succeed or not, continue to try deleting mount point */
rmdir(DFU_MOUNT_PATH);
}
fprintf(stdout, "finished\n");
}
-static char *get_partition_fstype(const char *devname)
-{
- blkid_tag_iterate tag_iter;
- blkid_dev dev;
- blkid_cache cache = NULL;
- const char *type, *value;
- int ret;
-
- ret = blkid_get_cache(&cache, NULL);
- if (ret < 0)
- return NULL;
-
- blkid_probe_all(cache);
-
- dev = blkid_get_dev(cache, devname, 0);
- if (!dev)
- return NULL;
-
- dev = blkid_verify(cache, dev);
- if (!dev)
- return NULL;
-
- tag_iter = blkid_tag_iterate_begin(dev);
- while (blkid_tag_next(tag_iter, &type, &value) == 0) {
- if (!strncmp(type, "TYPE", 4)) {
- char *fstype = strdup(value);
-
- blkid_tag_iterate_end(tag_iter);
- blkid_put_cache(cache);
- return fstype;
- }
- }
- blkid_tag_iterate_end(tag_iter);
- blkid_put_cache(cache);
-
- return NULL;
-}
-
static int dfu_start_entity(struct tfm_context *ctx, int idx, unsigned long size)
{
char **info = dfu_info[idx];
break;
case DFU_INFO_MODE_FILE:
{
+ int ret;
int path_prefix = strlen(DFU_MOUNT_PATH);
int path_suffix = strlen(info[DFU_INFO_PATH]);
int path_name = strlen(info[DFU_INFO_NAME]);
- char fstype;
- fstype = get_partition_fstype(info[DFU_INFO_PARTITION]);
- if (!fstype) {
- fprintf(stderr, "failed to get partition filesystem type: %s", info[DFU_INFO_PARTITION]);
+ ret = mount_dev(info[DFU_INFO_PARTITION]);
+ if (ret < 0)
return -EINVAL;
- }
-
- mount_dev(info[DFU_INFO_PARTITION], fstype);
- free(fstype);
file = malloc(path_prefix + path_suffix + path_name + 1);
- if (!file)
+ if (!file) {
+ umount_dev();
return -ENOMEM;
+ }
strncpy(file, DFU_MOUNT_PATH, path_prefix + 1);
strncat(file, info[DFU_INFO_PATH], path_suffix);