uboot/common/cmd_cramfs.c
<<
>>
Prefs
   1/*
   2 * This program is free software; you can redistribute it and/or
   3 * modify it under the terms of the GNU General Public License as
   4 * published by the Free Software Foundation; either version 2 of
   5 * the License, or (at your option) any later version.
   6 *
   7 * This program is distributed in the hope that it will be useful,
   8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
   9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10 * GNU General Public License for more details.
  11 *
  12 * You should have received a copy of the GNU General Public License
  13 * along with this program; if not, write to the Free Software
  14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  15 * MA 02111-1307 USA
  16 *
  17 * based on: cmd_jffs2.c
  18 *
  19 *      Add support for a CRAMFS located in RAM
  20 */
  21
  22
  23/*
  24 * CRAMFS support
  25 */
  26#include <common.h>
  27#include <command.h>
  28#include <malloc.h>
  29#include <linux/list.h>
  30#include <linux/ctype.h>
  31#include <jffs2/jffs2.h>
  32#include <jffs2/load_kernel.h>
  33#include <cramfs/cramfs_fs.h>
  34
  35/* enable/disable debugging messages */
  36#define DEBUG_CRAMFS
  37#undef  DEBUG_CRAMFS
  38
  39#ifdef  DEBUG_CRAMFS
  40# define DEBUGF(fmt, args...)   printf(fmt ,##args)
  41#else
  42# define DEBUGF(fmt, args...)
  43#endif
  44
  45#ifdef CONFIG_CRAMFS_CMDLINE
  46flash_info_t flash_info[1];
  47
  48#ifndef CONFIG_CMD_JFFS2
  49#include <linux/stat.h>
  50char *mkmodestr(unsigned long mode, char *str)
  51{
  52        static const char *l = "xwr";
  53        int mask = 1, i;
  54        char c;
  55
  56        switch (mode & S_IFMT) {
  57                case S_IFDIR:    str[0] = 'd'; break;
  58                case S_IFBLK:    str[0] = 'b'; break;
  59                case S_IFCHR:    str[0] = 'c'; break;
  60                case S_IFIFO:    str[0] = 'f'; break;
  61                case S_IFLNK:    str[0] = 'l'; break;
  62                case S_IFSOCK:   str[0] = 's'; break;
  63                case S_IFREG:    str[0] = '-'; break;
  64                default:         str[0] = '?';
  65        }
  66
  67        for(i = 0; i < 9; i++) {
  68                c = l[i%3];
  69                str[9-i] = (mode & mask)?c:'-';
  70                mask = mask<<1;
  71        }
  72
  73        if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  74        if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  75        if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  76        str[10] = '\0';
  77        return str;
  78}
  79#endif /* CONFIG_CMD_JFFS2 */
  80
  81extern int cramfs_check (struct part_info *info);
  82extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
  83extern int cramfs_ls (struct part_info *info, char *filename);
  84extern int cramfs_info (struct part_info *info);
  85
  86/***************************************************/
  87/* U-boot commands                                 */
  88/***************************************************/
  89
  90/**
  91 * Routine implementing fsload u-boot command. This routine tries to load
  92 * a requested file from cramfs filesystem at location 'cramfsaddr'.
  93 * cramfsaddr is an evironment variable.
  94 *
  95 * @param cmdtp command internal data
  96 * @param flag command flag
  97 * @param argc number of arguments supplied to the command
  98 * @param argv arguments list
  99 * @return 0 on success, 1 otherwise
 100 */
 101int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 102{
 103        char *filename;
 104        int size;
 105        ulong offset = load_addr;
 106
 107        struct part_info part;
 108        struct mtd_device dev;
 109        struct mtdids id;
 110
 111        ulong addr;
 112        addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
 113
 114        /* hack! */
 115        /* cramfs_* only supports NOR flash chips */
 116        /* fake the device type */
 117        id.type = MTD_DEV_TYPE_NOR;
 118        id.num = 0;
 119        dev.id = &id;
 120        part.dev = &dev;
 121        /* fake the address offset */
 122        part.offset = addr - flash_info[id.num].start[0];
 123
 124        /* pre-set Boot file name */
 125        if ((filename = getenv("bootfile")) == NULL) {
 126                filename = "uImage";
 127        }
 128
 129        if (argc == 2) {
 130                filename = argv[1];
 131        }
 132        if (argc == 3) {
 133                offset = simple_strtoul(argv[1], NULL, 0);
 134                load_addr = offset;
 135                filename = argv[2];
 136        }
 137
 138        size = 0;
 139        if (cramfs_check(&part))
 140                size = cramfs_load ((char *) offset, &part, filename);
 141
 142        if (size > 0) {
 143                char buf[10];
 144                printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
 145                        size, offset);
 146                sprintf(buf, "%x", size);
 147                setenv("filesize", buf);
 148        } else {
 149                printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
 150        }
 151
 152        return !(size > 0);
 153}
 154
 155/**
 156 * Routine implementing u-boot ls command which lists content of a given
 157 * directory at location 'cramfsaddr'.
 158 * cramfsaddr is an evironment variable.
 159 *
 160 * @param cmdtp command internal data
 161 * @param flag command flag
 162 * @param argc number of arguments supplied to the command
 163 * @param argv arguments list
 164 * @return 0 on success, 1 otherwise
 165 */
 166int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 167{
 168        char *filename = "/";
 169        int ret;
 170        struct part_info part;
 171        struct mtd_device dev;
 172        struct mtdids id;
 173
 174        ulong addr;
 175        addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
 176
 177        /* hack! */
 178        /* cramfs_* only supports NOR flash chips */
 179        /* fake the device type */
 180        id.type = MTD_DEV_TYPE_NOR;
 181        id.num = 0;
 182        dev.id = &id;
 183        part.dev = &dev;
 184        /* fake the address offset */
 185        part.offset = addr - flash_info[id.num].start[0];
 186
 187        if (argc == 2)
 188                filename = argv[1];
 189
 190        ret = 0;
 191        if (cramfs_check(&part))
 192                ret = cramfs_ls (&part, filename);
 193
 194        return ret ? 0 : 1;
 195}
 196
 197/* command line only */
 198
 199/***************************************************/
 200U_BOOT_CMD(
 201        cramfsload,     3,      0,      do_cramfs_load,
 202        "load binary file from a filesystem image",
 203        "[ off ] [ filename ]\n"
 204        "    - load binary file from address 'cramfsaddr'\n"
 205        "      with offset 'off'\n"
 206);
 207U_BOOT_CMD(
 208        cramfsls,       2,      1,      do_cramfs_ls,
 209        "list files in a directory (default /)",
 210        "[ directory ]\n"
 211        "    - list files in a directory.\n"
 212);
 213
 214#endif /* #ifdef CONFIG_CRAMFS_CMDLINE */
 215
 216/***************************************************/
 217