blob: 41f9eb8bd336747a54e3b5a3eb971fd8b3640ed6 [file] [log] [blame]
Uma Shankar71014b62012-05-25 21:21:44 +05301/*
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 * Ext4 Extent data structures are taken from original ext4 fs code
8 * as found in the linux kernel.
9 *
10 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
11 * Written by Alex Tomas <alex@clusterfs.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#ifndef __EXT4__
28#define __EXT4__
29#include <ext_common.h>
30
Simon Glass655306c2020-05-10 11:39:58 -060031struct disk_partition;
32
Stefan Brüns57db8992016-09-06 04:36:45 +020033#define EXT4_INDEX_FL 0x00001000 /* Inode uses hash tree index */
Sean Andersond90fa0b2023-10-14 16:47:50 -040034#define EXT4_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
Uma Shankar71014b62012-05-25 21:21:44 +053035#define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
36#define EXT4_EXT_MAGIC 0xf30a
Richard Weinberger9493a9a2024-07-29 23:37:31 +020037
38#define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
39#define EXT4_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
40#define EXT4_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
41#define EXT4_FEATURE_RO_COMPAT_HUGE_FILE 0x0008
42#define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0010
43#define EXT4_FEATURE_RO_COMPAT_DIR_NLINK 0x0020
44#define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE 0x0040
45#define EXT4_FEATURE_RO_COMPAT_QUOTA 0x0100
46#define EXT4_FEATURE_RO_COMPAT_BIGALLOC 0x0200
Sébastien Szymanskif7d49682019-03-22 09:33:52 +010047#define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM 0x0400
Richard Weinberger9493a9a2024-07-29 23:37:31 +020048
49#define EXT4_FEATURE_INCOMPAT_FILETYPE 0x0002
50#define EXT4_FEATURE_INCOMPAT_RECOVER 0x0004
Uma Shankar71014b62012-05-25 21:21:44 +053051#define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040
Tom Rini44720652016-07-22 17:59:11 -040052#define EXT4_FEATURE_INCOMPAT_64BIT 0x0080
Richard Weinberger9493a9a2024-07-29 23:37:31 +020053#define EXT4_FEATURE_INCOMPAT_MMP 0x0100
54#define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
55#define EXT4_FEATURE_INCOMPAT_CSUM_SEED 0x2000
56#define EXT4_FEATURE_INCOMPAT_ENCRYPT 0x10000
57
Uma Shankar71014b62012-05-25 21:21:44 +053058#define EXT4_INDIRECT_BLOCKS 12
59
Richard Weinberger9493a9a2024-07-29 23:37:31 +020060/*
61 * Incompat features supported by this implementation.
62 */
63#define EXT4_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE | \
64 EXT4_FEATURE_INCOMPAT_RECOVER | \
65 EXT4_FEATURE_INCOMPAT_EXTENTS | \
66 EXT4_FEATURE_INCOMPAT_64BIT | \
67 EXT4_FEATURE_INCOMPAT_FLEX_BG)
68
69/*
70 * Incompat features supported by this implementation only in a lazy
71 * way, good enough for reading files.
72 *
73 * - Multi mount protection (mmp) is not supported, but for read-only
74 * we get away with it.
75 * - Same for metadata_csum_seed and metadata_csum.
76 * - The implementation has also no clue about fscrypt, but it can read
77 * unencrypted files. Reading encrypted files will read garbage.
78 */
79#define EXT4_FEATURE_INCOMPAT_SUPP_LAZY_RO (EXT4_FEATURE_INCOMPAT_MMP | \
80 EXT4_FEATURE_INCOMPAT_CSUM_SEED | \
81 EXT4_FEATURE_INCOMPAT_ENCRYPT)
82
83/*
84 * Read-only compat features we support.
85 * If unknown ro compat features are detected, writing to the fs is denied.
86 */
87#define EXT4_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | \
88 EXT4_FEATURE_RO_COMPAT_LARGE_FILE | \
89 EXT4_FEATURE_RO_COMPAT_HUGE_FILE | \
90 EXT4_FEATURE_RO_COMPAT_GDT_CSUM | \
91 EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \
92 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
93
Uma Shankar71014b62012-05-25 21:21:44 +053094#define EXT4_BG_INODE_UNINIT 0x0001
95#define EXT4_BG_BLOCK_UNINIT 0x0002
96#define EXT4_BG_INODE_ZEROED 0x0004
97
98/*
99 * ext4_inode has i_block array (60 bytes total).
100 * The first 12 bytes store ext4_extent_header;
101 * the remainder stores an array of ext4_extent.
102 */
103
104/*
105 * This is the extent on-disk structure.
106 * It's used at the bottom of the tree.
107 */
108struct ext4_extent {
109 __le32 ee_block; /* first logical block extent covers */
110 __le16 ee_len; /* number of blocks covered by extent */
111 __le16 ee_start_hi; /* high 16 bits of physical block */
112 __le32 ee_start_lo; /* low 32 bits of physical block */
113};
114
115/*
116 * This is index on-disk structure.
117 * It's used at all the levels except the bottom.
118 */
119struct ext4_extent_idx {
120 __le32 ei_block; /* index covers logical blocks from 'block' */
121 __le32 ei_leaf_lo; /* pointer to the physical block of the next *
122 * level. leaf or next index could be there */
123 __le16 ei_leaf_hi; /* high 16 bits of physical block */
124 __u16 ei_unused;
125};
126
127/* Each block (leaves and indexes), even inode-stored has header. */
128struct ext4_extent_header {
129 __le16 eh_magic; /* probably will support different formats */
130 __le16 eh_entries; /* number of valid entries */
131 __le16 eh_max; /* capacity of store in entries */
132 __le16 eh_depth; /* has tree real underlying blocks? */
133 __le32 eh_generation; /* generation of the tree */
134};
135
136struct ext_filesystem {
137 /* Total Sector of partition */
138 uint64_t total_sect;
139 /* Block size of partition */
140 uint32_t blksz;
141 /* Inode size of partition */
142 uint32_t inodesz;
143 /* Sectors per Block */
144 uint32_t sect_perblk;
Stefan Brünsb2c28a22016-09-17 02:10:07 +0200145 /* Group Descriptor size */
146 uint16_t gdsize;
Uma Shankar71014b62012-05-25 21:21:44 +0530147 /* Group Descriptor Block Number */
148 uint32_t gdtable_blkno;
149 /* Total block groups of partition */
150 uint32_t no_blkgrp;
151 /* No of blocks required for bgdtable */
152 uint32_t no_blk_pergdt;
153 /* Superblock */
154 struct ext2_sblock *sb;
155 /* Block group descritpor table */
Uma Shankar71014b62012-05-25 21:21:44 +0530156 char *gdtable;
157
158 /* Block Bitmap Related */
159 unsigned char **blk_bmaps;
160 long int curr_blkno;
161 uint16_t first_pass_bbmap;
162
163 /* Inode Bitmap Related */
164 unsigned char **inode_bmaps;
165 int curr_inode_no;
166 uint16_t first_pass_ibmap;
167
168 /* Journal Related */
169
170 /* Block Device Descriptor */
Simon Glasse3394752016-02-29 15:25:34 -0700171 struct blk_desc *dev_desc;
Uma Shankar71014b62012-05-25 21:21:44 +0530172};
173
Stephen Warren02d6ca72019-01-30 12:58:05 -0700174struct ext_block_cache {
175 char *buf;
176 lbaint_t block;
177 int size;
178};
179
Uma Shankar71014b62012-05-25 21:21:44 +0530180extern struct ext2_data *ext4fs_root;
181extern struct ext2fs_node *ext4fs_file;
182
Stephen Warren4f8662d2012-10-22 06:43:50 +0000183#if defined(CONFIG_EXT4_WRITE)
Uma Shankara74a99a2012-05-25 21:22:49 +0530184extern struct ext2_inode *g_parent_inode;
185extern int gd_index;
186extern int gindex;
187
188int ext4fs_init(void);
189void ext4fs_deinit(void);
Stefan Brüns278f5d32016-09-06 04:36:41 +0200190int ext4fs_filename_unlink(char *filename);
Jean-Jacques Hiblotd1921362019-02-13 12:15:24 +0100191int ext4fs_write(const char *fname, const char *buffer,
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100192 unsigned long sizebytes, int type);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800193int ext4_write_file(const char *filename, void *buf, loff_t offset, loff_t len,
194 loff_t *actwrite);
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100195int ext4fs_create_link(const char *target, const char *fname);
Uma Shankara74a99a2012-05-25 21:22:49 +0530196#endif
197
Uma Shankar71014b62012-05-25 21:21:44 +0530198struct ext_filesystem *get_fs(void);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800199int ext4fs_open(const char *filename, loff_t *len);
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100200int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread);
Sean Andersonab125f52023-11-08 12:51:09 -0500201int ext4fs_mount(void);
Uma Shankar71014b62012-05-25 21:21:44 +0530202void ext4fs_close(void);
Łukasz Majewski900db5d2014-05-06 09:36:05 +0200203void ext4fs_reinit_global(void);
Uma Shankar71014b62012-05-25 21:21:44 +0530204int ext4fs_ls(const char *dirname);
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700205int ext4fs_exists(const char *filename);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800206int ext4fs_size(const char *filename, loff_t *size);
Uma Shankar71014b62012-05-25 21:21:44 +0530207void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200208int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600209void ext4fs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700210long int read_allocated_block(struct ext2_inode *inode, int fileblock,
211 struct ext_block_cache *cache);
Simon Glasse3394752016-02-29 15:25:34 -0700212int ext4fs_probe(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600213 struct disk_partition *fs_partition);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800214int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
215 loff_t *actread);
Egbert Eich7b1b2552013-05-01 01:13:19 +0000216int ext4_read_superblock(char *buffer);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100217int ext4fs_uuid(char *uuid_str);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700218void ext_cache_init(struct ext_block_cache *cache);
219void ext_cache_fini(struct ext_block_cache *cache);
220int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size);
Uma Shankar71014b62012-05-25 21:21:44 +0530221#endif