static int ubi_attach_mtd(const char *name) { int ret; int mtd_num, ubi_num, vid_off; int ubi_ctrl, ubi_dev; int vols, avail_lebs, leb_size; char path[128]; struct ubi_attach_req attach_req; struct ubi_mkvol_req mkvol_req; mtd_num = mtd_name_to_number(name); if (mtd_num == -1) { return -1; } for (ubi_num = 0; ubi_num < 4; ubi_num++) { sprintf(path, "/sys/class/ubi/ubi%d/mtd_num", ubi_num); ubi_dev = open(path, O_RDONLY); if (ubi_dev != -1) { ret = read(ubi_dev, path, sizeof(path)); close(ubi_dev); if (ret > 0 && mtd_num == atoi(path)) return ubi_num; } } ubi_ctrl = open(UBI_CTRL_DEV, O_RDONLY); if (ubi_ctrl == -1) { return -1; } memset(&attach_req, 0, sizeof(struct ubi_attach_req)); attach_req.ubi_num = UBI_DEV_NUM_AUTO; attach_req.mtd_num = mtd_num; attach_req.vid_hdr_offset = UBI_VID_OFFSET_AUTO; ret = ioctl(ubi_ctrl, UBI_IOCATT, &attach_req); if (ret == -1) { close(ubi_ctrl); return -1; } ubi_num = attach_req.ubi_num; vid_off = attach_req.vid_hdr_offset; vols = ubi_dev_read_int(ubi_num, "volumes_count", -1); if (vols == 0) { sprintf(path, "/dev/ubi%d", ubi_num); ret = wait_for_file(path, 50); ubi_dev = open(path, O_RDONLY); if (ubi_dev == -1) { close(ubi_ctrl); return ubi_num; } avail_lebs = ubi_dev_read_int(ubi_num, "avail_eraseblocks", 0); leb_size = ubi_dev_read_int(ubi_num, "eraseblock_size", 0); memset(&mkvol_req, 0, sizeof(struct ubi_mkvol_req)); mkvol_req.vol_id = UBI_VOL_NUM_AUTO; mkvol_req.alignment = 1; mkvol_req.bytes = (long long)avail_lebs * leb_size; mkvol_req.vol_type = UBI_DYNAMIC_VOLUME; ret = snprintf(mkvol_req.name, UBI_MAX_VOLUME_NAME + 1, "%s", name); mkvol_req.name_len = ret; ioctl(ubi_dev, UBI_IOCMKVOL, &mkvol_req); close(ubi_dev); } close(ubi_ctrl); return ubi_num; }
int ubi_mkvol_user(const char *mount_point) { //int fd; int ret; int ubi_num, ubi_dev,vols; int avail_lebs, leb_size; char path[128]; struct ubi_mkvol_req r; //size_t n; //memset(&r, 0, sizeof(struct ubi_mkvol_req)); if (!(!strcmp(mount_point, "/system") || !strcmp(mount_point, "/data") || !strcmp(mount_point, "/cache") || !strcmp(mount_point, "/.cache") || !strcmp(mount_point, "/custom"))) { LOGE("Invalid mount_point: %s\n", mount_point); return -1; } if (!strcmp(mount_point, "/system")) { ubi_num = 0; } if (!strcmp(mount_point, "/data")) { ubi_num = 1; } if (!strcmp(mount_point, "/cache") || !strcmp(mount_point, "/.cache")) { ubi_num = 2; } if (!strcmp(mount_point, "/custom")) { //ubi_num = 3; } vols = ubi_dev_read_int(ubi_num, "volumes_count", -1); if (vols == 0) { sprintf(path, "/dev/ubi%d", ubi_num); ubi_dev = open(path, O_RDONLY); if (ubi_dev == -1) { //close(ubi_ctrl); LOGE("failed to open attached UBI device\n"); return ubi_num; } avail_lebs = ubi_dev_read_int(ubi_num, "avail_eraseblocks", 0); leb_size = ubi_dev_read_int(ubi_num, "eraseblock_size", 0); //Make UBI volume memset(&r, 0, sizeof(struct ubi_mkvol_req)); r.vol_id = 0; //UBI_VOL_NUM_AUTO; r.alignment = 1; r.bytes = (long long)avail_lebs * leb_size; r.vol_type = UBI_DYNAMIC_VOLUME; ret = snprintf(r.name, UBI_MAX_VOLUME_NAME + 1, "%s", mount_point); r.name_len = ret; ret = ioctl(ubi_dev, UBI_IOCMKVOL, &r); close(ubi_dev); #ifdef UDEV_SETTLE_HACK // if (system("udevsettle") == -1) // return -1; usleep(100000); #endif if (ret == -1) { LOGE("failed to make UBI volume\n"); return ret; } } printf("make UBI volume success\n"); return 0; }
int ubi_rmvol_user(const char *mount_point) { //int fd, ret; int ret, ubi_num, ubi_dev,vols; int vol_id = 0; char path[128]; //memset(&r, 0, sizeof(struct ubi_mkvol_req)); if (!(!strcmp(mount_point, "/system") || !strcmp(mount_point, "/data") || !strcmp(mount_point, "/cache") || !strcmp(mount_point, "/.cache") || !strcmp(mount_point, "/custom"))) { LOGE("Invalid mount_point: %s\n", mount_point); return -1; } if (!strcmp(mount_point, "/system")) { ubi_num = 0; } if (!strcmp(mount_point, "/data")) { ubi_num = 1; } if (!strcmp(mount_point, "/cache") || !strcmp(mount_point, "/.cache")) { ubi_num = 2; } if (!strcmp(mount_point, "/custom")) { //ubi_num = 3; } vols = ubi_dev_read_int(ubi_num, "volumes_count", -1); if (vols == 0) { sprintf(path, "/dev/ubi%d", ubi_num); ubi_dev = open(path, O_RDONLY); if (ubi_dev == -1) { //close(ubi_ctrl); LOGE("failed to open attached UBI device\n"); return ubi_num; } /* //desc = desc; fd = open(ubi_dev, O_RDONLY); if (fd == -1) return -1; //sys_errmsg("cannot open \"%s\"", node); */ ret = ioctl(ubi_dev, UBI_IOCRMVOL, &vol_id); if (ret == -1) { close(ubi_dev); LOGE("failed to remove UBI volume\n"); return ret; } close(ubi_dev); #ifdef UDEV_SETTLE_HACK // if (system("udevsettle") == -1) // return -1; usleep(100000); #endif } printf("Remove UBI volume success\n"); return 0; }
int ubi_attach_mtd_user(const char *mount_point) { int ret; int vid_off; int ubi_ctrl=-1; int ubi_dev=-1; int vols, avail_lebs, leb_size; int32_t ubi_num, mtd_num, ubi_check; char path[128]; char name[128]; struct ubi_attach_req attach_req; struct ubi_mkvol_req mkvol_req; const MtdPartition *partition; const char* partition_name; int ubi_attached = 0; /* mtd_num = mtd_name_to_number(name); if (mtd_num == -1) { return -1; } */ if (!(!strcmp(mount_point, "/system") || !strcmp(mount_point, "/data") || !strcmp(mount_point, "/cache") || !strcmp(mount_point, "/.cache") || !strcmp(mount_point, "/custom"))) { LOGE("Invalid mount_point: %s\n", mount_point); return -1; } if (!strcmp(mount_point, "/system")) { ubi_num = 0; partition_name = "system"; } if (!strcmp(mount_point, "/data")) { ubi_num = 1; partition_name = "userdata"; } if (!strcmp(mount_point, "/cache") || !strcmp(mount_point, "/.cache")) { ubi_num = 2; partition_name = "cache"; } if (!strcmp(mount_point, "/custom")) { //ubi_num = 3; //mtd_num = 14; } mtd_scan_partitions(); partition = mtd_find_partition_by_name(partition_name); if (partition == NULL) { LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", partition_name, mount_point); return -1; } mtd_num = mtd_part_to_number(partition); printf("mount point[%s], mtd_num:%d \n", mount_point, mtd_num); //Check if device already attached for (ubi_check = 0; ubi_check < 4; ubi_check++) { sprintf(path, "/sys/class/ubi/ubi%d/mtd_num", ubi_check); ubi_dev = open(path, O_RDONLY); if (ubi_dev != -1) { ret = read(ubi_dev, path, sizeof(path)); close(ubi_dev); if (ret > 0 && mtd_num == atoi(path)) { printf("ubi%d already attached\n", ubi_check); ubi_attached = 1; //return ubi_check; } } } //If UBI device is already attached, skip it, just make UBI volume if (!ubi_attached) { ubi_ctrl = open(UBI_CTRL_DEV, O_RDONLY); printf("ubi_ctrl = %d\n", ubi_ctrl); if (ubi_ctrl == -1) { LOGE("failed to open UBI_CTRL_DEV\n"); return -1; } //attach UBI device to MTD printf("ubi_num = %d\n",ubi_num); memset(&attach_req, 0, sizeof(struct ubi_attach_req)); attach_req.ubi_num = ubi_num; attach_req.mtd_num = mtd_num; attach_req.vid_hdr_offset = UBI_VID_OFFSET_AUTO; ret = ioctl(ubi_ctrl, UBI_IOCATT, &attach_req); if (ret == -1) { close(ubi_ctrl); LOGE("failed to UBI_IOCATT\n"); return -1; } } //ubi_num = attach_req.ubi_num; vid_off = attach_req.vid_hdr_offset; vols = ubi_dev_read_int(ubi_num, "volumes_count", -1); if (vols == 0) { long long data_vol_size = 0; sprintf(path, "/dev/ubi%d", ubi_num); ubi_dev = open(path, O_RDONLY); if (ubi_dev == -1) { close(ubi_ctrl); LOGE("failed to open attached UBI device\n"); return ubi_num; } avail_lebs = ubi_dev_read_int(ubi_num, "avail_eraseblocks", 0); leb_size = ubi_dev_read_int(ubi_num, "eraseblock_size", 0); data_vol_size = (long long)avail_lebs * leb_size; #if defined(MTK_MLC_NAND_SUPPORT) #if defined(MTK_IPOH_SUPPORT) if (!strcmp(mount_point, "/data")) { data_vol_size -= BOARD_UBIFS_IPOH_VOLUME_SIZE; } #endif #endif memset(&mkvol_req, 0, sizeof(struct ubi_mkvol_req)); mkvol_req.vol_id = UBI_VOL_NUM_AUTO; mkvol_req.alignment = 1; mkvol_req.bytes = data_vol_size; mkvol_req.vol_type = UBI_DYNAMIC_VOLUME; ret = snprintf(mkvol_req.name, UBI_MAX_VOLUME_NAME + 1, "%s", name); mkvol_req.name_len = ret; ioctl(ubi_dev, UBI_IOCMKVOL, &mkvol_req); #if defined(MTK_MLC_NAND_SUPPORT) #if defined(MTK_IPOH_SUPPORT) if (!strcmp(mount_point, "/data")) { memset(&mkvol_req, 0, sizeof(struct ubi_mkvol_req)); mkvol_req.vol_id = UBI_VOL_NUM_AUTO; mkvol_req.alignment = 1; mkvol_req.bytes = (long long)BOARD_UBIFS_IPOH_VOLUME_SIZE; mkvol_req.vol_type = UBI_DYNAMIC_VOLUME; ret = snprintf(mkvol_req.name, UBI_MAX_VOLUME_NAME + 1, "%s", "ipoh"); mkvol_req.name_len = ret; ioctl(ubi_dev, UBI_IOCMKVOL, &mkvol_req); } #endif #endif close(ubi_dev); } close(ubi_ctrl); return ubi_num; }