wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Richard Jones, rjones@nexus-tech.net |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Boot support |
| 26 | */ |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <cmd_boot.h> |
| 30 | #include <cmd_autoscript.h> |
| 31 | #include <s_record.h> |
| 32 | #include <net.h> |
| 33 | #include <ata.h> |
| 34 | |
| 35 | #if (CONFIG_COMMANDS & CFG_CMD_FAT) |
| 36 | |
| 37 | #undef DEBUG |
| 38 | |
| 39 | #include <fat.h> |
| 40 | |
| 41 | extern block_dev_desc_t *ide_get_dev (int dev); |
| 42 | |
| 43 | int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 44 | { |
| 45 | long size; |
| 46 | unsigned long offset; |
| 47 | unsigned long count; |
| 48 | |
| 49 | if (argc < 3) { |
| 50 | printf ("usage:fatload <filename> <addr> [bytes]\n"); |
| 51 | return (0); |
| 52 | } |
| 53 | |
| 54 | offset = simple_strtoul (argv[2], NULL, 16); |
| 55 | if (argc == 4) |
| 56 | count = simple_strtoul (argv[3], NULL, 16); |
| 57 | else |
| 58 | count = 0; |
| 59 | |
| 60 | size = file_fat_read (argv[1], (unsigned char *) offset, count); |
| 61 | |
| 62 | printf ("%ld bytes read\n", size); |
| 63 | |
| 64 | return size; |
| 65 | } |
| 66 | |
| 67 | int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 68 | { |
| 69 | char *filename = "/"; |
| 70 | int ret; |
| 71 | |
| 72 | if (argc == 2) |
| 73 | ret = file_fat_ls (argv[1]); |
| 74 | else |
| 75 | ret = file_fat_ls (filename); |
| 76 | |
| 77 | return (ret); |
| 78 | } |
| 79 | |
| 80 | int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 81 | { |
| 82 | int ret; |
| 83 | |
| 84 | ret = 0; |
| 85 | |
| 86 | printf ("FAT info: %d\n", file_fat_detectfs ()); |
| 87 | |
| 88 | return (ret); |
| 89 | } |
| 90 | |
| 91 | #ifdef NOT_IMPLEMENTED_YET |
| 92 | /* find first device whose first partition is a DOS filesystem */ |
| 93 | int find_fat_partition (void) |
| 94 | { |
| 95 | int i, j; |
| 96 | block_dev_desc_t *dev_desc; |
| 97 | unsigned char *part_table; |
| 98 | unsigned char buffer[ATA_BLOCKSIZE]; |
| 99 | |
| 100 | for (i = 0; i < CFG_IDE_MAXDEVICE; i++) { |
| 101 | dev_desc = ide_get_dev (i); |
| 102 | if (!dev_desc) { |
| 103 | debug ("couldn't get ide device!\n"); |
| 104 | return (-1); |
| 105 | } |
| 106 | if (dev_desc->part_type == PART_TYPE_DOS) { |
| 107 | if (dev_desc-> |
| 108 | block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) { |
| 109 | debug ("can't perform block_read!\n"); |
| 110 | return (-1); |
| 111 | } |
| 112 | part_table = &buffer[0x1be]; /* start with partition #4 */ |
| 113 | for (j = 0; j < 4; j++) { |
| 114 | if ((part_table[4] == 1 || /* 12-bit FAT */ |
| 115 | part_table[4] == 4 || /* 16-bit FAT */ |
| 116 | part_table[4] == 6) && /* > 32Meg part */ |
| 117 | part_table[0] == 0x80) { /* bootable? */ |
| 118 | curr_dev = i; |
| 119 | part_offset = part_table[11]; |
| 120 | part_offset <<= 8; |
| 121 | part_offset |= part_table[10]; |
| 122 | part_offset <<= 8; |
| 123 | part_offset |= part_table[9]; |
| 124 | part_offset <<= 8; |
| 125 | part_offset |= part_table[8]; |
| 126 | debug ("found partition start at %ld\n", part_offset); |
| 127 | return (0); |
| 128 | } |
| 129 | part_table += 16; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | debug ("no valid devices found!\n"); |
| 135 | return (-1); |
| 136 | } |
| 137 | |
| 138 | int |
| 139 | do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[]) |
| 140 | { |
| 141 | __u8 block[1024]; |
| 142 | int ret; |
| 143 | int bknum; |
| 144 | |
| 145 | ret = 0; |
| 146 | |
| 147 | if (argc != 2) { |
| 148 | printf ("needs an argument!\n"); |
| 149 | return (0); |
| 150 | } |
| 151 | |
| 152 | bknum = simple_strtoul (argv[1], NULL, 10); |
| 153 | |
| 154 | if (disk_read (0, bknum, block) != 0) { |
| 155 | printf ("Error: reading block\n"); |
| 156 | return -1; |
| 157 | } |
| 158 | printf ("FAT dump: %d\n", bknum); |
| 159 | hexdump (512, block); |
| 160 | |
| 161 | return (ret); |
| 162 | } |
| 163 | |
| 164 | int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr) |
| 165 | { |
| 166 | ulong tot; |
| 167 | block_dev_desc_t *dev_desc; |
| 168 | |
| 169 | if (curr_dev < 0) { |
| 170 | if (find_fat_partition () != 0) |
| 171 | return (-1); |
| 172 | } |
| 173 | |
| 174 | dev_desc = ide_get_dev (curr_dev); |
| 175 | if (!dev_desc) { |
| 176 | debug ("couldn't get ide device\n"); |
| 177 | return (-1); |
| 178 | } |
| 179 | |
| 180 | tot = dev_desc->block_read (0, startblock + part_offset, |
| 181 | getsize, (ulong *) bufptr); |
| 182 | |
| 183 | /* should we do this here? |
| 184 | flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz); |
| 185 | */ |
| 186 | |
| 187 | if (tot == getsize) |
| 188 | return (0); |
| 189 | |
| 190 | debug ("unable to read from device!\n"); |
| 191 | |
| 192 | return (-1); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | static int isprint (unsigned char ch) |
| 197 | { |
| 198 | if (ch >= 32 && ch < 127) |
| 199 | return (1); |
| 200 | |
| 201 | return (0); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void hexdump (int cnt, unsigned char *data) |
| 206 | { |
| 207 | int i; |
| 208 | int run; |
| 209 | int offset; |
| 210 | |
| 211 | offset = 0; |
| 212 | while (cnt) { |
| 213 | printf ("%04X : ", offset); |
| 214 | if (cnt >= 16) |
| 215 | run = 16; |
| 216 | else |
| 217 | run = cnt; |
| 218 | cnt -= run; |
| 219 | for (i = 0; i < run; i++) |
| 220 | printf ("%02X ", (unsigned int) data[i]); |
| 221 | printf (": "); |
| 222 | for (i = 0; i < run; i++) |
| 223 | printf ("%c", isprint (data[i]) ? data[i] : '.'); |
| 224 | printf ("\n"); |
| 225 | data = &data[16]; |
| 226 | offset += run; |
| 227 | } |
| 228 | } |
| 229 | #endif /* NOT_IMPLEMENTED_YET */ |
| 230 | |
| 231 | #endif /* CFG_CMD_FAT */ |