blob: 9cdf28e79f1a56399e2ca8f0982a4993a4ffc4cc [file] [log] [blame]
Stuart Longlandfd1000b2016-02-23 15:51:26 +10001/*
2 * (c) Copyright 2016 by VRT Technology
3 *
4 * Author:
5 * Stuart Longland <stuartl@vrt.com.au>
6 *
7 * Based on FAT environment driver
8 * (c) Copyright 2011 by Tigris Elektronik GmbH
9 *
10 * Author:
11 * Maximilian Schwerin <mvs@tigris.de>
12 *
13 * and EXT4 filesystem implementation
14 * (C) Copyright 2011 - 2012 Samsung Electronics
15 * EXT4 filesystem implementation in Uboot by
16 * Uma Shankar <uma.shankar@samsung.com>
17 * Manjunatha C Achar <a.manjunatha@samsung.com>
18 *
19 * SPDX-License-Identifier: GPL-2.0+
20 */
21
22#include <common.h>
23
24#include <command.h>
25#include <environment.h>
26#include <linux/stddef.h>
27#include <malloc.h>
Andrej Rosanobd62e242016-06-21 17:54:25 +020028#include <memalign.h>
Stuart Longlandfd1000b2016-02-23 15:51:26 +100029#include <search.h>
30#include <errno.h>
31#include <ext4fs.h>
32#include <mmc.h>
33
Stuart Longlandfd1000b2016-02-23 15:51:26 +100034DECLARE_GLOBAL_DATA_PTR;
35
Stuart Longlandfd1000b2016-02-23 15:51:26 +100036#ifdef CONFIG_CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -060037static int env_ext4_save(void)
Stuart Longlandfd1000b2016-02-23 15:51:26 +100038{
39 env_t env_new;
Andrej Rosanobd62e242016-06-21 17:54:25 +020040 struct blk_desc *dev_desc = NULL;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100041 disk_partition_t info;
42 int dev, part;
43 int err;
44
45 err = env_export(&env_new);
46 if (err)
47 return err;
48
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +010049 part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
50 CONFIG_ENV_EXT4_DEVICE_AND_PART,
51 &dev_desc, &info, 1);
Stuart Longlandfd1000b2016-02-23 15:51:26 +100052 if (part < 0)
53 return 1;
54
Andrej Rosanobd62e242016-06-21 17:54:25 +020055 dev = dev_desc->devnum;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100056 ext4fs_set_blk_dev(dev_desc, &info);
57
58 if (!ext4fs_mount(info.size)) {
59 printf("\n** Unable to use %s %s for saveenv **\n",
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +010060 CONFIG_ENV_EXT4_INTERFACE,
61 CONFIG_ENV_EXT4_DEVICE_AND_PART);
Stuart Longlandfd1000b2016-02-23 15:51:26 +100062 return 1;
63 }
64
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +010065 err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)&env_new,
66 sizeof(env_t));
Stuart Longlandfd1000b2016-02-23 15:51:26 +100067 ext4fs_close();
68
69 if (err == -1) {
70 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +010071 CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
72 part);
Stuart Longlandfd1000b2016-02-23 15:51:26 +100073 return 1;
74 }
75
76 puts("done\n");
77 return 0;
78}
79#endif /* CONFIG_CMD_SAVEENV */
80
Simon Glassc5951992017-08-03 12:22:17 -060081static int env_ext4_load(void)
Stuart Longlandfd1000b2016-02-23 15:51:26 +100082{
83 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Andrej Rosanobd62e242016-06-21 17:54:25 +020084 struct blk_desc *dev_desc = NULL;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100085 disk_partition_t info;
86 int dev, part;
87 int err;
Andrej Rosanobd62e242016-06-21 17:54:25 +020088 loff_t off;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100089
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +010090 part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
91 CONFIG_ENV_EXT4_DEVICE_AND_PART,
92 &dev_desc, &info, 1);
Stuart Longlandfd1000b2016-02-23 15:51:26 +100093 if (part < 0)
94 goto err_env_relocate;
95
Andrej Rosanobd62e242016-06-21 17:54:25 +020096 dev = dev_desc->devnum;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100097 ext4fs_set_blk_dev(dev_desc, &info);
98
99 if (!ext4fs_mount(info.size)) {
100 printf("\n** Unable to use %s %s for loading the env **\n",
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +0100101 CONFIG_ENV_EXT4_INTERFACE,
102 CONFIG_ENV_EXT4_DEVICE_AND_PART);
Stuart Longlandfd1000b2016-02-23 15:51:26 +1000103 goto err_env_relocate;
104 }
105
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +0100106 err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
107 &off);
Stuart Longlandfd1000b2016-02-23 15:51:26 +1000108 ext4fs_close();
109
110 if (err == -1) {
111 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
Jorge Ramirez-Ortiz1087a792018-01-10 11:33:48 +0100112 CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
113 part);
Stuart Longlandfd1000b2016-02-23 15:51:26 +1000114 goto err_env_relocate;
115 }
116
117 env_import(buf, 1);
Simon Glassc5951992017-08-03 12:22:17 -0600118 return 0;
Stuart Longlandfd1000b2016-02-23 15:51:26 +1000119
120err_env_relocate:
121 set_default_env(NULL);
Simon Glassc5951992017-08-03 12:22:17 -0600122
123 return -EIO;
Stuart Longlandfd1000b2016-02-23 15:51:26 +1000124}
Simon Glass4415f1d2017-08-03 12:21:58 -0600125
126U_BOOT_ENV_LOCATION(ext4) = {
127 .location = ENVL_EXT4,
Simon Glassac358be2017-08-03 12:22:03 -0600128 ENV_NAME("EXT4")
Simon Glasse5bce242017-08-03 12:22:01 -0600129 .load = env_ext4_load,
130 .save = env_save_ptr(env_ext4_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600131};