183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20649cd0dSSimon Glass /*
30649cd0dSSimon Glass * (c) Copyright 2016 by VRT Technology
40649cd0dSSimon Glass *
50649cd0dSSimon Glass * Author:
60649cd0dSSimon Glass * Stuart Longland <stuartl@vrt.com.au>
70649cd0dSSimon Glass *
80649cd0dSSimon Glass * Based on FAT environment driver
90649cd0dSSimon Glass * (c) Copyright 2011 by Tigris Elektronik GmbH
100649cd0dSSimon Glass *
110649cd0dSSimon Glass * Author:
120649cd0dSSimon Glass * Maximilian Schwerin <mvs@tigris.de>
130649cd0dSSimon Glass *
140649cd0dSSimon Glass * and EXT4 filesystem implementation
150649cd0dSSimon Glass * (C) Copyright 2011 - 2012 Samsung Electronics
160649cd0dSSimon Glass * EXT4 filesystem implementation in Uboot by
170649cd0dSSimon Glass * Uma Shankar <uma.shankar@samsung.com>
180649cd0dSSimon Glass * Manjunatha C Achar <a.manjunatha@samsung.com>
190649cd0dSSimon Glass */
200649cd0dSSimon Glass
210649cd0dSSimon Glass #include <common.h>
220649cd0dSSimon Glass
230649cd0dSSimon Glass #include <command.h>
240649cd0dSSimon Glass #include <environment.h>
250649cd0dSSimon Glass #include <linux/stddef.h>
260649cd0dSSimon Glass #include <malloc.h>
270649cd0dSSimon Glass #include <memalign.h>
280649cd0dSSimon Glass #include <search.h>
290649cd0dSSimon Glass #include <errno.h>
300649cd0dSSimon Glass #include <ext4fs.h>
310649cd0dSSimon Glass #include <mmc.h>
320649cd0dSSimon Glass
330649cd0dSSimon Glass #ifdef CONFIG_CMD_SAVEENV
env_ext4_save(void)34e5bce247SSimon Glass static int env_ext4_save(void)
350649cd0dSSimon Glass {
360649cd0dSSimon Glass env_t env_new;
370649cd0dSSimon Glass struct blk_desc *dev_desc = NULL;
380649cd0dSSimon Glass disk_partition_t info;
390649cd0dSSimon Glass int dev, part;
400649cd0dSSimon Glass int err;
410649cd0dSSimon Glass
420649cd0dSSimon Glass err = env_export(&env_new);
430649cd0dSSimon Glass if (err)
440649cd0dSSimon Glass return err;
450649cd0dSSimon Glass
461087a794SJorge Ramirez-Ortiz part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
471087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_DEVICE_AND_PART,
480649cd0dSSimon Glass &dev_desc, &info, 1);
490649cd0dSSimon Glass if (part < 0)
500649cd0dSSimon Glass return 1;
510649cd0dSSimon Glass
520649cd0dSSimon Glass dev = dev_desc->devnum;
530649cd0dSSimon Glass ext4fs_set_blk_dev(dev_desc, &info);
540649cd0dSSimon Glass
550649cd0dSSimon Glass if (!ext4fs_mount(info.size)) {
560649cd0dSSimon Glass printf("\n** Unable to use %s %s for saveenv **\n",
571087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_INTERFACE,
581087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_DEVICE_AND_PART);
590649cd0dSSimon Glass return 1;
600649cd0dSSimon Glass }
610649cd0dSSimon Glass
621087a794SJorge Ramirez-Ortiz err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)&env_new,
631087a794SJorge Ramirez-Ortiz sizeof(env_t));
640649cd0dSSimon Glass ext4fs_close();
650649cd0dSSimon Glass
660649cd0dSSimon Glass if (err == -1) {
670649cd0dSSimon Glass printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
681087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
691087a794SJorge Ramirez-Ortiz part);
700649cd0dSSimon Glass return 1;
710649cd0dSSimon Glass }
720649cd0dSSimon Glass
730649cd0dSSimon Glass puts("done\n");
740649cd0dSSimon Glass return 0;
750649cd0dSSimon Glass }
760649cd0dSSimon Glass #endif /* CONFIG_CMD_SAVEENV */
770649cd0dSSimon Glass
env_ext4_load(void)78c5951991SSimon Glass static int env_ext4_load(void)
790649cd0dSSimon Glass {
800649cd0dSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
810649cd0dSSimon Glass struct blk_desc *dev_desc = NULL;
820649cd0dSSimon Glass disk_partition_t info;
830649cd0dSSimon Glass int dev, part;
840649cd0dSSimon Glass int err;
850649cd0dSSimon Glass loff_t off;
860649cd0dSSimon Glass
8795058fbbSHeinrich Schuchardt #ifdef CONFIG_MMC
8826862b4aSFaiz Abbas if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc"))
8926862b4aSFaiz Abbas mmc_initialize(NULL);
9095058fbbSHeinrich Schuchardt #endif
9126862b4aSFaiz Abbas
921087a794SJorge Ramirez-Ortiz part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
931087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_DEVICE_AND_PART,
940649cd0dSSimon Glass &dev_desc, &info, 1);
950649cd0dSSimon Glass if (part < 0)
960649cd0dSSimon Glass goto err_env_relocate;
970649cd0dSSimon Glass
980649cd0dSSimon Glass dev = dev_desc->devnum;
990649cd0dSSimon Glass ext4fs_set_blk_dev(dev_desc, &info);
1000649cd0dSSimon Glass
1010649cd0dSSimon Glass if (!ext4fs_mount(info.size)) {
1020649cd0dSSimon Glass printf("\n** Unable to use %s %s for loading the env **\n",
1031087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_INTERFACE,
1041087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_DEVICE_AND_PART);
1050649cd0dSSimon Glass goto err_env_relocate;
1060649cd0dSSimon Glass }
1070649cd0dSSimon Glass
1081087a794SJorge Ramirez-Ortiz err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
1091087a794SJorge Ramirez-Ortiz &off);
1100649cd0dSSimon Glass ext4fs_close();
1110649cd0dSSimon Glass
1120649cd0dSSimon Glass if (err == -1) {
1130649cd0dSSimon Glass printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
1141087a794SJorge Ramirez-Ortiz CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
1151087a794SJorge Ramirez-Ortiz part);
1160649cd0dSSimon Glass goto err_env_relocate;
1170649cd0dSSimon Glass }
1180649cd0dSSimon Glass
1192166ebf7SSimon Goldschmidt return env_import(buf, 1);
1200649cd0dSSimon Glass
1210649cd0dSSimon Glass err_env_relocate:
122*c5d548a9SYaniv Levinsky set_default_env(NULL, 0);
123c5951991SSimon Glass
124c5951991SSimon Glass return -EIO;
1250649cd0dSSimon Glass }
1264415f1d1SSimon Glass
1274415f1d1SSimon Glass U_BOOT_ENV_LOCATION(ext4) = {
1284415f1d1SSimon Glass .location = ENVL_EXT4,
129ac358bebSSimon Glass ENV_NAME("EXT4")
130e5bce247SSimon Glass .load = env_ext4_load,
131e5bce247SSimon Glass .save = env_save_ptr(env_ext4_save),
1324415f1d1SSimon Glass };
133