Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 1 | /* |
| 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 Rosano | bd62e24 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 28 | #include <memalign.h> |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 29 | #include <search.h> |
| 30 | #include <errno.h> |
| 31 | #include <ext4fs.h> |
| 32 | #include <mmc.h> |
| 33 | |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 36 | #ifdef CONFIG_CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 37 | static int env_ext4_save(void) |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 38 | { |
| 39 | env_t env_new; |
Andrej Rosano | bd62e24 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 40 | struct blk_desc *dev_desc = NULL; |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 41 | 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-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 49 | part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE, |
| 50 | CONFIG_ENV_EXT4_DEVICE_AND_PART, |
| 51 | &dev_desc, &info, 1); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 52 | if (part < 0) |
| 53 | return 1; |
| 54 | |
Andrej Rosano | bd62e24 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 55 | dev = dev_desc->devnum; |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 56 | 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-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 60 | CONFIG_ENV_EXT4_INTERFACE, |
| 61 | CONFIG_ENV_EXT4_DEVICE_AND_PART); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 62 | return 1; |
| 63 | } |
| 64 | |
Jorge Ramirez-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 65 | err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)&env_new, |
| 66 | sizeof(env_t)); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 67 | ext4fs_close(); |
| 68 | |
| 69 | if (err == -1) { |
| 70 | printf("\n** Unable to write \"%s\" from %s%d:%d **\n", |
Jorge Ramirez-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 71 | CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev, |
| 72 | part); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | puts("done\n"); |
| 77 | return 0; |
| 78 | } |
| 79 | #endif /* CONFIG_CMD_SAVEENV */ |
| 80 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 81 | static int env_ext4_load(void) |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 82 | { |
| 83 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Andrej Rosano | bd62e24 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 84 | struct blk_desc *dev_desc = NULL; |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 85 | disk_partition_t info; |
| 86 | int dev, part; |
| 87 | int err; |
Andrej Rosano | bd62e24 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 88 | loff_t off; |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 89 | |
Faiz Abbas | 26862b4 | 2018-02-12 19:24:31 +0530 | [diff] [blame] | 90 | if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc")) |
| 91 | mmc_initialize(NULL); |
| 92 | |
Jorge Ramirez-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 93 | part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE, |
| 94 | CONFIG_ENV_EXT4_DEVICE_AND_PART, |
| 95 | &dev_desc, &info, 1); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 96 | if (part < 0) |
| 97 | goto err_env_relocate; |
| 98 | |
Andrej Rosano | bd62e24 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 99 | dev = dev_desc->devnum; |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 100 | ext4fs_set_blk_dev(dev_desc, &info); |
| 101 | |
| 102 | if (!ext4fs_mount(info.size)) { |
| 103 | printf("\n** Unable to use %s %s for loading the env **\n", |
Jorge Ramirez-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 104 | CONFIG_ENV_EXT4_INTERFACE, |
| 105 | CONFIG_ENV_EXT4_DEVICE_AND_PART); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 106 | goto err_env_relocate; |
| 107 | } |
| 108 | |
Jorge Ramirez-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 109 | err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE, |
| 110 | &off); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 111 | ext4fs_close(); |
| 112 | |
| 113 | if (err == -1) { |
| 114 | printf("\n** Unable to read \"%s\" from %s%d:%d **\n", |
Jorge Ramirez-Ortiz | 1087a79 | 2018-01-10 11:33:48 +0100 | [diff] [blame] | 115 | CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev, |
| 116 | part); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 117 | goto err_env_relocate; |
| 118 | } |
| 119 | |
Simon Goldschmidt | 2166ebf | 2018-01-31 14:47:12 +0100 | [diff] [blame] | 120 | return env_import(buf, 1); |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 121 | |
| 122 | err_env_relocate: |
| 123 | set_default_env(NULL); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 124 | |
| 125 | return -EIO; |
Stuart Longland | fd1000b | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 126 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 127 | |
| 128 | U_BOOT_ENV_LOCATION(ext4) = { |
| 129 | .location = ENVL_EXT4, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 130 | ENV_NAME("EXT4") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 131 | .load = env_ext4_load, |
| 132 | .save = env_save_ptr(env_ext4_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 133 | }; |