Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 - 2012 Samsung Electronics |
| 3 | * EXT4 filesystem implementation in Uboot by |
| 4 | * Uma Shankar <uma.shankar@samsung.com> |
| 5 | * Manjunatha C Achar <a.manjunatha@samsung.com> |
| 6 | * |
| 7 | * ext4ls and ext4load : Based on ext2 ls and load support in Uboot. |
| 8 | * Ext4 read optimization taken from Open-Moko |
| 9 | * Qi bootloader |
| 10 | * |
| 11 | * (C) Copyright 2004 |
| 12 | * esd gmbh <www.esd-electronics.com> |
| 13 | * Reinhard Arlt <reinhard.arlt@esd-electronics.com> |
| 14 | * |
| 15 | * based on code from grub2 fs/ext2.c and fs/fshelp.c by |
| 16 | * GRUB -- GRand Unified Bootloader |
| 17 | * Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
| 18 | * |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 19 | * ext4write : Based on generic ext4 protocol. |
| 20 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 21 | * SPDX-License-Identifier: GPL-2.0+ |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 25 | #include <ext_common.h> |
| 26 | #include <ext4fs.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 27 | #include "ext4_common.h" |
| 28 | |
| 29 | int ext4fs_symlinknest; |
Rob Herring | 30f7fd7 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 30 | struct ext_filesystem ext_fs; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 31 | |
| 32 | struct ext_filesystem *get_fs(void) |
| 33 | { |
Rob Herring | 30f7fd7 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 34 | return &ext_fs; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot) |
| 38 | { |
| 39 | if ((node != &ext4fs_root->diropen) && (node != currroot)) |
| 40 | free(node); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Taken from openmoko-kernel mailing list: By Andy green |
| 45 | * Optimized read file API : collects and defers contiguous sector |
| 46 | * reads into one potentially more efficient larger sequential read action |
| 47 | */ |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 48 | int ext4fs_read_file(struct ext2fs_node *node, loff_t pos, |
| 49 | loff_t len, char *buf, loff_t *actread) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 50 | { |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 51 | struct ext_filesystem *fs = get_fs(); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 52 | int i; |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 53 | lbaint_t blockcnt; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 54 | int log2blksz = fs->dev_desc->log2blksz; |
| 55 | int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz; |
| 56 | int blocksize = (1 << (log2_fs_blocksize + log2blksz)); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 57 | unsigned int filesize = __le32_to_cpu(node->inode.size); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 58 | lbaint_t previous_block_number = -1; |
| 59 | lbaint_t delayed_start = 0; |
| 60 | lbaint_t delayed_extent = 0; |
| 61 | lbaint_t delayed_skipfirst = 0; |
| 62 | lbaint_t delayed_next = 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 63 | char *delayed_buf = NULL; |
| 64 | short status; |
| 65 | |
| 66 | /* Adjust len so it we can't read past the end of the file. */ |
| 67 | if (len > filesize) |
| 68 | len = filesize; |
| 69 | |
| 70 | blockcnt = ((len + pos) + blocksize - 1) / blocksize; |
| 71 | |
| 72 | for (i = pos / blocksize; i < blockcnt; i++) { |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 73 | lbaint_t blknr; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 74 | int blockoff = pos % blocksize; |
| 75 | int blockend = blocksize; |
| 76 | int skipfirst = 0; |
| 77 | blknr = read_allocated_block(&(node->inode), i); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 78 | if (blknr < 0) |
| 79 | return -1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 80 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 81 | blknr = blknr << log2_fs_blocksize; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 82 | |
| 83 | /* Last block. */ |
| 84 | if (i == blockcnt - 1) { |
| 85 | blockend = (len + pos) % blocksize; |
| 86 | |
| 87 | /* The last portion is exactly blocksize. */ |
| 88 | if (!blockend) |
| 89 | blockend = blocksize; |
| 90 | } |
| 91 | |
| 92 | /* First block. */ |
| 93 | if (i == pos / blocksize) { |
| 94 | skipfirst = blockoff; |
| 95 | blockend -= skipfirst; |
| 96 | } |
| 97 | if (blknr) { |
| 98 | int status; |
| 99 | |
| 100 | if (previous_block_number != -1) { |
| 101 | if (delayed_next == blknr) { |
| 102 | delayed_extent += blockend; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 103 | delayed_next += blockend >> log2blksz; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 104 | } else { /* spill */ |
| 105 | status = ext4fs_devread(delayed_start, |
| 106 | delayed_skipfirst, |
| 107 | delayed_extent, |
| 108 | delayed_buf); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 109 | if (status == 0) |
| 110 | return -1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 111 | previous_block_number = blknr; |
| 112 | delayed_start = blknr; |
| 113 | delayed_extent = blockend; |
| 114 | delayed_skipfirst = skipfirst; |
| 115 | delayed_buf = buf; |
| 116 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 117 | (blockend >> log2blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 118 | } |
| 119 | } else { |
| 120 | previous_block_number = blknr; |
| 121 | delayed_start = blknr; |
| 122 | delayed_extent = blockend; |
| 123 | delayed_skipfirst = skipfirst; |
| 124 | delayed_buf = buf; |
| 125 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 126 | (blockend >> log2blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 127 | } |
| 128 | } else { |
| 129 | if (previous_block_number != -1) { |
| 130 | /* spill */ |
| 131 | status = ext4fs_devread(delayed_start, |
| 132 | delayed_skipfirst, |
| 133 | delayed_extent, |
| 134 | delayed_buf); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 135 | if (status == 0) |
| 136 | return -1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 137 | previous_block_number = -1; |
| 138 | } |
| 139 | memset(buf, 0, blocksize - skipfirst); |
| 140 | } |
| 141 | buf += blocksize - skipfirst; |
| 142 | } |
| 143 | if (previous_block_number != -1) { |
| 144 | /* spill */ |
| 145 | status = ext4fs_devread(delayed_start, |
| 146 | delayed_skipfirst, delayed_extent, |
| 147 | delayed_buf); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 148 | if (status == 0) |
| 149 | return -1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 150 | previous_block_number = -1; |
| 151 | } |
| 152 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 153 | *actread = len; |
| 154 | return 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int ext4fs_ls(const char *dirname) |
| 158 | { |
| 159 | struct ext2fs_node *dirnode; |
| 160 | int status; |
| 161 | |
| 162 | if (dirname == NULL) |
| 163 | return 0; |
| 164 | |
| 165 | status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode, |
| 166 | FILETYPE_DIRECTORY); |
| 167 | if (status != 1) { |
| 168 | printf("** Can not find directory. **\n"); |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | ext4fs_iterate_dir(dirnode, NULL, NULL, NULL); |
| 173 | ext4fs_free_node(dirnode, &ext4fs_root->diropen); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 178 | int ext4fs_exists(const char *filename) |
| 179 | { |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 180 | loff_t file_len; |
| 181 | int ret; |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 182 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 183 | ret = ext4fs_open(filename, &file_len); |
| 184 | return ret == 0; |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 187 | int ext4fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 188 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 189 | return ext4fs_open(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 190 | } |
| 191 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 192 | int ext4fs_read(char *buf, loff_t len, loff_t *actread) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 193 | { |
| 194 | if (ext4fs_root == NULL || ext4fs_file == NULL) |
| 195 | return 0; |
| 196 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 197 | return ext4fs_read_file(ext4fs_file, 0, len, buf, actread); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 198 | } |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 199 | |
| 200 | int ext4fs_probe(block_dev_desc_t *fs_dev_desc, |
| 201 | disk_partition_t *fs_partition) |
| 202 | { |
| 203 | ext4fs_set_blk_dev(fs_dev_desc, fs_partition); |
| 204 | |
| 205 | if (!ext4fs_mount(fs_partition->size)) { |
| 206 | ext4fs_close(); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 213 | int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
| 214 | loff_t *len_read) |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 215 | { |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 216 | loff_t file_len; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 217 | int ret; |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 218 | |
| 219 | if (offset != 0) { |
| 220 | printf("** Cannot support non-zero offset **\n"); |
| 221 | return -1; |
| 222 | } |
| 223 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 224 | ret = ext4fs_open(filename, &file_len); |
| 225 | if (ret < 0) { |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 226 | printf("** File not found %s **\n", filename); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | if (len == 0) |
| 231 | len = file_len; |
| 232 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 233 | return ext4fs_read(buf, len, len_read); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 234 | } |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 235 | |
| 236 | int ext4fs_uuid(char *uuid_str) |
| 237 | { |
| 238 | if (ext4fs_root == NULL) |
| 239 | return -1; |
| 240 | |
| 241 | #ifdef CONFIG_LIB_UUID |
| 242 | uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id, |
| 243 | uuid_str, UUID_STR_FORMAT_STD); |
| 244 | |
| 245 | return 0; |
| 246 | #else |
| 247 | return -ENOSYS; |
| 248 | #endif |
| 249 | } |