disk: Implement flashing mmc images

This commit is contained in:
Pali Rohár committed 2021-05-01 20:07:49 +02:00
1 parent 38f83f8db4
commit b090564a85
2 files changed
+75 -15

No files matched your search

+74 -14
View File
@@ -289,12 +289,68 @@ int disk_dump_dev(int fd, const char * file) {
} }
int disk_flash_dev(int fd, const char * file) { int disk_flash_dev(int fd, struct image * image) {
ERROR("Not implemented yet"); uint64_t blksize;
(void)fd; size_t need, sent;
(void)file; ssize_t size;
return -1;
if ( image->type != IMAGE_MMC )
ERROR_RETURN("Only mmc images are supported", -1);
printf("Writing image to block device...\n");
#ifdef __linux__
if ( ioctl(fd, BLKGETSIZE64, &blksize) != 0 ) {
ERROR_INFO("Cannot get size of block device");
return -1;
}
#else
blksize = lseek(fd, 0, SEEK_END);
if ( (off_t)blksize == (off_t)-1 ) {
ERROR_INFO("Cannot get size of block device");
return -1;
}
if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) {
ERROR_INFO("Cannot seek to begin of block device");
return -1;
}
#endif
if ( blksize == 0 )
ERROR_RETURN("Block device has zero size", -1);
if ( image->size > blksize )
ERROR_RETURN("Image is too big", -1);
sent = 0;
printf_progressbar(0, image->size);
while ( sent < image->size ) {
need = image->size - sent;
if ( need > sizeof(global_buf) )
need = sizeof(global_buf);
size = image_read(image, global_buf, need);
if ( size == 0 ) {
PRINTF_ERROR("Failed to read image");
return -1;
}
if ( ! simulate ) {
if ( write(fd, global_buf, size) != size ) {
PRINTF_ERROR("Writing image failed");
return -1;
}
}
sent += size;
printf_progressbar(sent, image->size);
}
return 0;
} }
@@ -412,16 +468,14 @@ int disk_init(struct usb_device_info * dev) {
maj2 = tmp; maj2 = tmp;
} }
/* TODO: change 1 to 0 when disk_flash_dev will be implemented */
/* RX-51, RM-680 and RM-696 export MyDocs in first usb device and just first partion, so host system see whole device without MBR table */ /* RX-51, RM-680 and RM-696 export MyDocs in first usb device and just first partion, so host system see whole device without MBR table */
if ( dev->device == DEVICE_RX_51 || dev->device == DEVICE_RM_680 || dev->device == DEVICE_RM_696 ) if ( dev->device == DEVICE_RX_51 || dev->device == DEVICE_RM_680 || dev->device == DEVICE_RM_696 )
fd = disk_open_dev(maj1, min1, -1, 1); fd = disk_open_dev(maj1, min1, -1, simulate ? 1 : 0);
/* Other devices can export SD card as first partition and export whole mmc device, so host system will see MBR table */ /* Other devices can export SD card as first partition and export whole mmc device, so host system will see MBR table */
else if ( maj2 != -1 && min2 != -1 ) else if ( maj2 != -1 && min2 != -1 )
fd = disk_open_dev(maj2, min2, 1, 1); fd = disk_open_dev(maj2, min2, 1, simulate ? 1 : 0);
else else
fd = disk_open_dev(maj1, min1, 1, 1); fd = disk_open_dev(maj1, min1, 1, simulate ? 1 : 0);
if ( fd < 0 ) if ( fd < 0 )
return -1; return -1;
@@ -454,10 +508,16 @@ enum device disk_get_device(struct usb_device_info * dev) {
int disk_flash_image(struct usb_device_info * dev, struct image * image) { int disk_flash_image(struct usb_device_info * dev, struct image * image) {
ERROR("Not implemented yet"); int ret;
(void)dev;
(void)image; printf("Flash image:\n");
return -1; image_print_info(image);
ret = disk_flash_dev(dev->data, image);
if ( ret == 0 )
printf("Done\n");
return ret;
} }
+1 -1
View File
@@ -30,7 +30,7 @@ enum device disk_get_device(struct usb_device_info * dev);
int disk_open_dev(int maj, int min, int partition, int readonly); int disk_open_dev(int maj, int min, int partition, int readonly);
int disk_dump_dev(int fd, const char * file); int disk_dump_dev(int fd, const char * file);
int disk_flash_dev(int fd, const char * file); int disk_flash_dev(int fd, struct image * image);
int disk_flash_image(struct usb_device_info * dev, struct image * image); int disk_flash_image(struct usb_device_info * dev, struct image * image);
int disk_dump_image(struct usb_device_info * dev, enum image_type image, const char * file); int disk_dump_image(struct usb_device_info * dev, enum image_type image, const char * file);