2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define _LARGEFILE64_SOURCE
30 #define _FILE_OFFSET_BITS 64
32 #include <ext4_config.h>
33 #include <ext4_blockdev.h>
34 #include <ext4_errno.h>
43 #include <sys/socket.h>
46 /**@brief Default filename.*/
47 static const char *fname = "ext2";
49 /**@brief Image block size.*/
50 #define EXT4_FILEDEV_BSIZE 512
52 /**@brief Image file descriptor.*/
53 static FILE *dev_file;
55 #define DROP_LINUXCACHE_BUFFERS 0
57 /**********************BLOCKDEV INTERFACE**************************************/
58 static int file_dev_open(struct ext4_blockdev *bdev);
59 static int file_dev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
61 static int file_dev_bwrite(struct ext4_blockdev *bdev, const void *buf,
62 uint64_t blk_id, uint32_t blk_cnt);
63 static int file_dev_close(struct ext4_blockdev *bdev);
65 /******************************************************************************/
66 EXT4_BLOCKDEV_STATIC_INSTANCE(file_dev, EXT4_FILEDEV_BSIZE, 0, file_dev_open,
67 file_dev_bread, file_dev_bwrite, file_dev_close, 0, 0);
69 /******************************************************************************/
70 static int file_dev_open(struct ext4_blockdev *bdev)
72 dev_file = fopen(fname, "r+b");
77 /*No buffering at file.*/
81 /* The fseek/ftell approach to finding the device's size does not seem
82 * to work on macOS so do it this way instead.
85 if (ioctl(fileno(dev_file), DKIOCGETBLOCKCOUNT, §ors) < 0) {
89 uint32_t sector_size = 0;
90 if (ioctl(fileno(dev_file), DKIOCGETBLOCKSIZE, §or_size) < 0) {
95 off_t size = sectors * sector_size;
97 if (fseeko(dev_file, 0, SEEK_END))
100 off_t size = ftello(dev_file);
103 file_dev.part_offset = 0;
104 file_dev.part_size = size;
105 file_dev.bdif->ph_bcnt = file_dev.part_size / file_dev.bdif->ph_bsize;
110 /******************************************************************************/
112 static int file_dev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
115 if (fseeko(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET))
119 if (!fread(buf, bdev->bdif->ph_bsize * blk_cnt, 1, dev_file))
125 static void drop_cache(void)
127 #if defined(__linux__) && DROP_LINUXCACHE_BUFFERS
132 fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
133 write(fd, data, sizeof(char));
138 /******************************************************************************/
139 static int file_dev_bwrite(struct ext4_blockdev *bdev, const void *buf,
140 uint64_t blk_id, uint32_t blk_cnt)
142 if (fseeko(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET))
146 if (!fwrite(buf, bdev->bdif->ph_bsize * blk_cnt, 1, dev_file))
152 /******************************************************************************/
153 static int file_dev_close(struct ext4_blockdev *bdev)
159 /******************************************************************************/
160 struct ext4_blockdev *file_dev_get(void)
164 /******************************************************************************/
165 void file_dev_name_set(const char *n)
169 /******************************************************************************/