Stuart Longland | d1f4063 | 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 | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 28 | #include <memalign.h> |
Stuart Longland | d1f4063 | 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 | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 36 | #ifdef CONFIG_CMD_SAVEENV |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 37 | static int env_ext4_save(void) |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 38 | { |
| 39 | env_t env_new; |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 40 | struct blk_desc *dev_desc = NULL; |
Stuart Longland | d1f4063 | 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 | |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 49 | part = blk_get_device_part_str(EXT4_ENV_INTERFACE, |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 50 | EXT4_ENV_DEVICE_AND_PART, |
| 51 | &dev_desc, &info, 1); |
| 52 | if (part < 0) |
| 53 | return 1; |
| 54 | |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 55 | dev = dev_desc->devnum; |
Stuart Longland | d1f4063 | 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", |
| 60 | EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART); |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t)); |
| 65 | ext4fs_close(); |
| 66 | |
| 67 | if (err == -1) { |
| 68 | printf("\n** Unable to write \"%s\" from %s%d:%d **\n", |
| 69 | EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part); |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | puts("done\n"); |
| 74 | return 0; |
| 75 | } |
| 76 | #endif /* CONFIG_CMD_SAVEENV */ |
| 77 | |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 78 | static int env_ext4_load(void) |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 79 | { |
| 80 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 81 | struct blk_desc *dev_desc = NULL; |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 82 | disk_partition_t info; |
| 83 | int dev, part; |
| 84 | int err; |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 85 | loff_t off; |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 86 | |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 87 | part = blk_get_device_part_str(EXT4_ENV_INTERFACE, |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 88 | EXT4_ENV_DEVICE_AND_PART, |
| 89 | &dev_desc, &info, 1); |
| 90 | if (part < 0) |
| 91 | goto err_env_relocate; |
| 92 | |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 93 | dev = dev_desc->devnum; |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 94 | ext4fs_set_blk_dev(dev_desc, &info); |
| 95 | |
| 96 | if (!ext4fs_mount(info.size)) { |
| 97 | printf("\n** Unable to use %s %s for loading the env **\n", |
| 98 | EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART); |
| 99 | goto err_env_relocate; |
| 100 | } |
| 101 | |
Andrej Rosano | b776347 | 2016-06-21 17:54:25 +0200 | [diff] [blame] | 102 | err = ext4_read_file(EXT4_ENV_FILE, buf, 0, CONFIG_ENV_SIZE, &off); |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 103 | ext4fs_close(); |
| 104 | |
| 105 | if (err == -1) { |
| 106 | printf("\n** Unable to read \"%s\" from %s%d:%d **\n", |
| 107 | EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part); |
| 108 | goto err_env_relocate; |
| 109 | } |
| 110 | |
| 111 | env_import(buf, 1); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 112 | return 0; |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 113 | |
| 114 | err_env_relocate: |
| 115 | set_default_env(NULL); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 116 | |
| 117 | return -EIO; |
Stuart Longland | d1f4063 | 2016-02-23 15:51:26 +1000 | [diff] [blame] | 118 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 119 | |
| 120 | U_BOOT_ENV_LOCATION(ext4) = { |
| 121 | .location = ENVL_EXT4, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 122 | ENV_NAME("EXT4") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 123 | .load = env_ext4_load, |
| 124 | .save = env_save_ptr(env_ext4_save), |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 125 | }; |