Uma Shankar | a159643 | 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 | ed34f34 | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 19 | * ext4write : Based on generic ext4 protocol. |
| 20 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 21 | * SPDX-License-Identifier: GPL-2.0+ |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 25 | #include <ext_common.h> |
| 26 | #include <ext4fs.h> |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 27 | #include "ext4_common.h" |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 28 | #include <div64.h> |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 29 | |
| 30 | int ext4fs_symlinknest; |
Rob Herring | 9450106 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 31 | struct ext_filesystem ext_fs; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 32 | |
| 33 | struct ext_filesystem *get_fs(void) |
| 34 | { |
Rob Herring | 9450106 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 35 | return &ext_fs; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot) |
| 39 | { |
| 40 | if ((node != &ext4fs_root->diropen) && (node != currroot)) |
| 41 | free(node); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Taken from openmoko-kernel mailing list: By Andy green |
| 46 | * Optimized read file API : collects and defers contiguous sector |
| 47 | * reads into one potentially more efficient larger sequential read action |
| 48 | */ |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 49 | int ext4fs_read_file(struct ext2fs_node *node, loff_t pos, |
| 50 | loff_t len, char *buf, loff_t *actread) |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 51 | { |
Egbert Eich | 50ce4c0 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 52 | struct ext_filesystem *fs = get_fs(); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 53 | int i; |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 54 | lbaint_t blockcnt; |
Egbert Eich | 50ce4c0 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 55 | int log2blksz = fs->dev_desc->log2blksz; |
| 56 | int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz; |
| 57 | int blocksize = (1 << (log2_fs_blocksize + log2blksz)); |
Michael Walle | 7f101be | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 58 | unsigned int filesize = le32_to_cpu(node->inode.size); |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 59 | lbaint_t previous_block_number = -1; |
| 60 | lbaint_t delayed_start = 0; |
| 61 | lbaint_t delayed_extent = 0; |
| 62 | lbaint_t delayed_skipfirst = 0; |
| 63 | lbaint_t delayed_next = 0; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 64 | char *delayed_buf = NULL; |
| 65 | short status; |
| 66 | |
| 67 | /* Adjust len so it we can't read past the end of the file. */ |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 68 | if (len + pos > filesize) |
| 69 | len = (filesize - pos); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 70 | |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 71 | blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 72 | |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 73 | for (i = lldiv(pos, blocksize); i < blockcnt; i++) { |
Lokesh Vutla | 509b498 | 2017-04-26 16:58:22 +0530 | [diff] [blame] | 74 | long int blknr; |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 75 | int blockoff = pos - (blocksize * i); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 76 | int blockend = blocksize; |
| 77 | int skipfirst = 0; |
| 78 | blknr = read_allocated_block(&(node->inode), i); |
Tom Rini | 715b56f | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 79 | if (blknr < 0) |
| 80 | return -1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 81 | |
Egbert Eich | 50ce4c0 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 82 | blknr = blknr << log2_fs_blocksize; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 83 | |
| 84 | /* Last block. */ |
| 85 | if (i == blockcnt - 1) { |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 86 | blockend = (len + pos) - (blocksize * i); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 87 | |
| 88 | /* The last portion is exactly blocksize. */ |
| 89 | if (!blockend) |
| 90 | blockend = blocksize; |
| 91 | } |
| 92 | |
| 93 | /* First block. */ |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 94 | if (i == lldiv(pos, blocksize)) { |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 95 | skipfirst = blockoff; |
| 96 | blockend -= skipfirst; |
| 97 | } |
| 98 | if (blknr) { |
| 99 | int status; |
| 100 | |
| 101 | if (previous_block_number != -1) { |
| 102 | if (delayed_next == blknr) { |
| 103 | delayed_extent += blockend; |
Egbert Eich | 50ce4c0 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 104 | delayed_next += blockend >> log2blksz; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 105 | } else { /* spill */ |
| 106 | status = ext4fs_devread(delayed_start, |
| 107 | delayed_skipfirst, |
| 108 | delayed_extent, |
| 109 | delayed_buf); |
Tom Rini | 715b56f | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 110 | if (status == 0) |
| 111 | return -1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 112 | previous_block_number = blknr; |
| 113 | delayed_start = blknr; |
| 114 | delayed_extent = blockend; |
| 115 | delayed_skipfirst = skipfirst; |
| 116 | delayed_buf = buf; |
| 117 | delayed_next = blknr + |
Egbert Eich | 50ce4c0 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 118 | (blockend >> log2blksz); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 119 | } |
| 120 | } else { |
| 121 | previous_block_number = blknr; |
| 122 | delayed_start = blknr; |
| 123 | delayed_extent = blockend; |
| 124 | delayed_skipfirst = skipfirst; |
| 125 | delayed_buf = buf; |
| 126 | delayed_next = blknr + |
Egbert Eich | 50ce4c0 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 127 | (blockend >> log2blksz); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 128 | } |
| 129 | } else { |
| 130 | if (previous_block_number != -1) { |
| 131 | /* spill */ |
| 132 | status = ext4fs_devread(delayed_start, |
| 133 | delayed_skipfirst, |
| 134 | delayed_extent, |
| 135 | delayed_buf); |
Tom Rini | 715b56f | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 136 | if (status == 0) |
| 137 | return -1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 138 | previous_block_number = -1; |
| 139 | } |
| 140 | memset(buf, 0, blocksize - skipfirst); |
| 141 | } |
| 142 | buf += blocksize - skipfirst; |
| 143 | } |
| 144 | if (previous_block_number != -1) { |
| 145 | /* spill */ |
| 146 | status = ext4fs_devread(delayed_start, |
| 147 | delayed_skipfirst, delayed_extent, |
| 148 | delayed_buf); |
Tom Rini | 715b56f | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 149 | if (status == 0) |
| 150 | return -1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 151 | previous_block_number = -1; |
| 152 | } |
| 153 | |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 154 | *actread = len; |
| 155 | return 0; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int ext4fs_ls(const char *dirname) |
| 159 | { |
| 160 | struct ext2fs_node *dirnode; |
| 161 | int status; |
| 162 | |
| 163 | if (dirname == NULL) |
| 164 | return 0; |
| 165 | |
| 166 | status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode, |
| 167 | FILETYPE_DIRECTORY); |
| 168 | if (status != 1) { |
| 169 | printf("** Can not find directory. **\n"); |
Tom Rini | fa9ca8a | 2017-09-26 22:43:45 -0400 | [diff] [blame] | 170 | ext4fs_free_node(dirnode, &ext4fs_root->diropen); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | ext4fs_iterate_dir(dirnode, NULL, NULL, NULL); |
| 175 | ext4fs_free_node(dirnode, &ext4fs_root->diropen); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Stephen Warren | 55af5c9 | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 180 | int ext4fs_exists(const char *filename) |
| 181 | { |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 182 | loff_t file_len; |
| 183 | int ret; |
Stephen Warren | 55af5c9 | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 184 | |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 185 | ret = ext4fs_open(filename, &file_len); |
| 186 | return ret == 0; |
Stephen Warren | 55af5c9 | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 189 | int ext4fs_size(const char *filename, loff_t *size) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 190 | { |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 191 | return ext4fs_open(filename, size); |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 192 | } |
| 193 | |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 194 | int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread) |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 195 | { |
| 196 | if (ext4fs_root == NULL || ext4fs_file == NULL) |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 197 | return -1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 198 | |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 199 | return ext4fs_read_file(ext4fs_file, offset, len, buf, actread); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 200 | } |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 201 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 202 | int ext4fs_probe(struct blk_desc *fs_dev_desc, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 203 | disk_partition_t *fs_partition) |
| 204 | { |
| 205 | ext4fs_set_blk_dev(fs_dev_desc, fs_partition); |
| 206 | |
| 207 | if (!ext4fs_mount(fs_partition->size)) { |
| 208 | ext4fs_close(); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 215 | int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
| 216 | loff_t *len_read) |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 217 | { |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 218 | loff_t file_len; |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 219 | int ret; |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 220 | |
Suriyan Ramasami | 9f12cd0 | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 221 | ret = ext4fs_open(filename, &file_len); |
| 222 | if (ret < 0) { |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 223 | printf("** File not found %s **\n", filename); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | if (len == 0) |
| 228 | len = file_len; |
| 229 | |
Stefan Brüns | 66a47ff | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 230 | return ext4fs_read(buf, offset, len, len_read); |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 231 | } |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 232 | |
| 233 | int ext4fs_uuid(char *uuid_str) |
| 234 | { |
| 235 | if (ext4fs_root == NULL) |
| 236 | return -1; |
| 237 | |
| 238 | #ifdef CONFIG_LIB_UUID |
| 239 | uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id, |
| 240 | uuid_str, UUID_STR_FORMAT_STD); |
| 241 | |
| 242 | return 0; |
| 243 | #else |
| 244 | return -ENOSYS; |
| 245 | #endif |
| 246 | } |