cbfs: Record the start address in cbfs_priv
[oweals/u-boot.git] / fs / fs.c
diff --git a/fs/fs.c b/fs/fs.c
index 62165d5c57010c8bd4d665fd977c0c0fbbecbe44..ad4caaeb1ec9c1f7f502df20d0dce92df6471b1a 100644 (file)
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -3,9 +3,13 @@
  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
  */
 
+#include <command.h>
 #include <config.h>
 #include <errno.h>
 #include <common.h>
+#include <env.h>
+#include <lmb.h>
+#include <log.h>
 #include <mapmem.h>
 #include <part.h>
 #include <ext4fs.h>
 #include <asm/io.h>
 #include <div64.h>
 #include <linux/math64.h>
+#include <efi_loader.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
 static struct blk_desc *fs_dev_desc;
 static int fs_dev_part;
-static disk_partition_t fs_partition;
+static struct disk_partition fs_partition;
 static int fs_type = FS_TYPE_ANY;
 
 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
-                                     disk_partition_t *fs_partition)
+                                     struct disk_partition *fs_partition)
 {
        printf("** Unrecognized filesystem type **\n");
        return -1;
@@ -90,6 +95,11 @@ static inline int fs_write_unsupported(const char *filename, void *buf,
        return -1;
 }
 
+static inline int fs_ln_unsupported(const char *filename, const char *target)
+{
+       return -1;
+}
+
 static inline void fs_close_unsupported(void)
 {
 }
@@ -105,6 +115,11 @@ static inline int fs_opendir_unsupported(const char *filename,
        return -EACCES;
 }
 
+static inline int fs_unlink_unsupported(const char *filename)
+{
+       return -1;
+}
+
 static inline int fs_mkdir_unsupported(const char *dirname)
 {
        return -1;
@@ -123,7 +138,7 @@ struct fstype_info {
         */
        bool null_dev_desc_ok;
        int (*probe)(struct blk_desc *fs_dev_desc,
-                    disk_partition_t *fs_partition);
+                    struct disk_partition *fs_partition);
        int (*ls)(const char *dirname);
        int (*exists)(const char *filename);
        int (*size)(const char *filename, loff_t *size);
@@ -147,7 +162,9 @@ struct fstype_info {
        int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
        /* see fs_closedir() */
        void (*closedir)(struct fs_dir_stream *dirs);
+       int (*unlink)(const char *filename);
        int (*mkdir)(const char *dirname);
+       int (*ln)(const char *filename, const char *target);
 };
 
 static struct fstype_info fstypes[] = {
@@ -162,19 +179,24 @@ static struct fstype_info fstypes[] = {
                .exists = fat_exists,
                .size = fat_size,
                .read = fat_read_file,
-#ifdef CONFIG_FAT_WRITE
+#if CONFIG_IS_ENABLED(FAT_WRITE)
                .write = file_fat_write,
+               .unlink = fat_unlink,
+               .mkdir = fat_mkdir,
 #else
                .write = fs_write_unsupported,
+               .unlink = fs_unlink_unsupported,
+               .mkdir = fs_mkdir_unsupported,
 #endif
                .uuid = fs_uuid_unsupported,
                .opendir = fat_opendir,
                .readdir = fat_readdir,
                .closedir = fat_closedir,
-               .mkdir = fs_mkdir_unsupported,
+               .ln = fs_ln_unsupported,
        },
 #endif
-#ifdef CONFIG_FS_EXT4
+
+#if CONFIG_IS_ENABLED(FS_EXT4)
        {
                .fstype = FS_TYPE_EXT,
                .name = "ext4",
@@ -187,11 +209,14 @@ static struct fstype_info fstypes[] = {
                .read = ext4_read_file,
 #ifdef CONFIG_CMD_EXT4_WRITE
                .write = ext4_write_file,
+               .ln = ext4fs_create_link,
 #else
                .write = fs_write_unsupported,
+               .ln = fs_ln_unsupported,
 #endif
                .uuid = ext4fs_uuid,
                .opendir = fs_opendir_unsupported,
+               .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
        },
 #endif
@@ -209,7 +234,9 @@ static struct fstype_info fstypes[] = {
                .write = fs_write_sandbox,
                .uuid = fs_uuid_unsupported,
                .opendir = fs_opendir_unsupported,
+               .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
+               .ln = fs_ln_unsupported,
        },
 #endif
 #ifdef CONFIG_CMD_UBIFS
@@ -226,7 +253,9 @@ static struct fstype_info fstypes[] = {
                .write = fs_write_unsupported,
                .uuid = fs_uuid_unsupported,
                .opendir = fs_opendir_unsupported,
+               .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
+               .ln = fs_ln_unsupported,
        },
 #endif
 #ifdef CONFIG_FS_BTRFS
@@ -243,7 +272,9 @@ static struct fstype_info fstypes[] = {
                .write = fs_write_unsupported,
                .uuid = btrfs_uuid,
                .opendir = fs_opendir_unsupported,
+               .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
+               .ln = fs_ln_unsupported,
        },
 #endif
        {
@@ -259,7 +290,9 @@ static struct fstype_info fstypes[] = {
                .write = fs_write_unsupported,
                .uuid = fs_uuid_unsupported,
                .opendir = fs_opendir_unsupported,
+               .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
+               .ln = fs_ln_unsupported,
        },
 };
 
@@ -277,6 +310,19 @@ static struct fstype_info *fs_get_info(int fstype)
        return info;
 }
 
+/**
+ * fs_get_type() - Get type of current filesystem
+ *
+ * Return: filesystem type
+ *
+ * Returns filesystem type representing the current filesystem, or
+ * FS_TYPE_ANY for any unrecognised filesystem.
+ */
+int fs_get_type(void)
+{
+       return fs_type;
+}
+
 /**
  * fs_get_type_name() - Get type of current filesystem
  *
@@ -351,6 +397,7 @@ int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
        for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
                if (!info->probe(fs_dev_desc, &fs_partition)) {
                        fs_type = info->fstype;
+                       fs_dev_part = part;
                        return 0;
                }
        }
@@ -358,7 +405,7 @@ int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
        return -1;
 }
 
-static void fs_close(void)
+void fs_close(void)
 {
        struct fstype_info *info = fs_get_info(fs_type);
 
@@ -382,7 +429,6 @@ int fs_ls(const char *dirname)
 
        ret = info->ls(dirname);
 
-       fs_type = FS_TYPE_ANY;
        fs_close();
 
        return ret;
@@ -414,13 +460,56 @@ int fs_size(const char *filename, loff_t *size)
        return ret;
 }
 
-int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
-           loff_t *actread)
+#ifdef CONFIG_LMB
+/* Check if a file may be read to the given address */
+static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
+                            loff_t len, struct fstype_info *info)
+{
+       struct lmb lmb;
+       int ret;
+       loff_t size;
+       loff_t read_len;
+
+       /* get the actual size of the file */
+       ret = info->size(filename, &size);
+       if (ret)
+               return ret;
+       if (offset >= size) {
+               /* offset >= EOF, no bytes will be written */
+               return 0;
+       }
+       read_len = size - offset;
+
+       /* limit to 'len' if it is smaller */
+       if (len && len < read_len)
+               read_len = len;
+
+       lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
+       lmb_dump_all(&lmb);
+
+       if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
+               return 0;
+
+       printf("** Reading file would overwrite reserved memory **\n");
+       return -ENOSPC;
+}
+#endif
+
+static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
+                   int do_lmb_check, loff_t *actread)
 {
        struct fstype_info *info = fs_get_info(fs_type);
        void *buf;
        int ret;
 
+#ifdef CONFIG_LMB
+       if (do_lmb_check) {
+               ret = fs_read_lmb_check(filename, addr, offset, len, info);
+               if (ret)
+                       return ret;
+       }
+#endif
+
        /*
         * We don't actually know how many bytes are being read, since len==0
         * means read the whole file.
@@ -437,6 +526,12 @@ int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
        return ret;
 }
 
+int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
+           loff_t *actread)
+{
+       return _fs_read(filename, addr, offset, len, 0, actread);
+}
+
 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
             loff_t *actwrite)
 {
@@ -509,6 +604,18 @@ void fs_closedir(struct fs_dir_stream *dirs)
        fs_close();
 }
 
+int fs_unlink(const char *filename)
+{
+       int ret;
+
+       struct fstype_info *info = fs_get_info(fs_type);
+
+       ret = info->unlink(filename);
+
+       fs_close();
+
+       return ret;
+}
 
 int fs_mkdir(const char *dirname)
 {
@@ -518,14 +625,29 @@ int fs_mkdir(const char *dirname)
 
        ret = info->mkdir(dirname);
 
-       fs_type = FS_TYPE_ANY;
        fs_close();
 
        return ret;
 }
 
-int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
-               int fstype)
+int fs_ln(const char *fname, const char *target)
+{
+       struct fstype_info *info = fs_get_info(fs_type);
+       int ret;
+
+       ret = info->ln(fname, target);
+
+       if (ret < 0) {
+               printf("** Unable to create link %s -> %s **\n", fname, target);
+               ret = -1;
+       }
+       fs_close();
+
+       return ret;
+}
+
+int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+           int fstype)
 {
        loff_t size;
 
@@ -543,8 +665,8 @@ int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
        return 0;
 }
 
-int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
-               int fstype)
+int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+           int fstype)
 {
        unsigned long addr;
        const char *addr_str;
@@ -593,8 +715,12 @@ int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
        else
                pos = 0;
 
+#ifdef CONFIG_CMD_BOOTEFI
+       efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
+                       (argc > 4) ? argv[4] : "");
+#endif
        time = get_timer(0);
-       ret = fs_read(filename, addr, pos, bytes, &len_read);
+       ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
        time = get_timer(time);
        if (ret < 0)
                return 1;
@@ -613,8 +739,8 @@ int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
        return 0;
 }
 
-int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
-       int fstype)
+int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+         int fstype)
 {
        if (argc < 2)
                return CMD_RET_USAGE;
@@ -639,8 +765,8 @@ int file_exists(const char *dev_type, const char *dev_part, const char *file,
        return fs_exists(file);
 }
 
-int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
-               int fstype)
+int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+           int fstype)
 {
        unsigned long addr;
        const char *filename;
@@ -681,8 +807,8 @@ int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
        return 0;
 }
 
-int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
-               int fstype)
+int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+              int fstype)
 {
        int ret;
        char uuid[37];
@@ -706,7 +832,7 @@ int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
        return CMD_RET_SUCCESS;
 }
 
-int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
        struct fstype_info *info;
 
@@ -723,10 +849,27 @@ int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        else
                printf("%s\n", info->name);
 
+       fs_close();
+
        return CMD_RET_SUCCESS;
 }
 
-int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
+int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+         int fstype)
+{
+       if (argc != 4)
+               return CMD_RET_USAGE;
+
+       if (fs_set_blk_dev(argv[1], argv[2], fstype))
+               return 1;
+
+       if (fs_unlink(argv[3]))
+               return 1;
+
+       return 0;
+}
+
+int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
             int fstype)
 {
        int ret;
@@ -745,3 +888,18 @@ int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
 
        return 0;
 }
+
+int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+         int fstype)
+{
+       if (argc != 5)
+               return CMD_RET_USAGE;
+
+       if (fs_set_blk_dev(argv[1], argv[2], fstype))
+               return 1;
+
+       if (fs_ln(argv[3], argv[4]))
+               return 1;
+
+       return 0;
+}