2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* common.c - common wrapper functions between U-Boot and the host
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2007-07-05 16:01:42 +00:00
|
|
|
|
2007-10-20 14:56:29 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Common wrapper functions between U-Boot and the host
|
|
|
|
*/
|
2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* These are host includes. Never include any U-Boot header
|
|
|
|
* files here...
|
|
|
|
*/
|
2007-07-05 16:01:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <errno.h>
|
2007-07-05 16:01:51 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/select.h>
|
2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* ...except the ones needed to connect with U-Boot
|
|
|
|
*/
|
|
|
|
#include <asm/arch/linux.h>
|
|
|
|
#include <asm/arch/hostfile.h>
|
2007-07-05 16:01:42 +00:00
|
|
|
|
|
|
|
static struct termios term_orig, term_vi;
|
2007-07-05 16:02:04 +00:00
|
|
|
static char erase_char; /* the users erase character */
|
2007-07-05 16:01:42 +00:00
|
|
|
|
|
|
|
static void rawmode(void)
|
|
|
|
{
|
|
|
|
tcgetattr(0, &term_orig);
|
|
|
|
term_vi = term_orig;
|
2007-07-05 16:01:59 +00:00
|
|
|
term_vi.c_lflag &= (~ICANON & ~ECHO & ~ISIG);
|
2007-07-05 16:01:42 +00:00
|
|
|
term_vi.c_iflag &= (~IXON & ~ICRNL);
|
2007-07-05 16:01:59 +00:00
|
|
|
term_vi.c_oflag |= (ONLCR);
|
2007-07-05 16:01:42 +00:00
|
|
|
term_vi.c_cc[VMIN] = 1;
|
|
|
|
term_vi.c_cc[VTIME] = 0;
|
|
|
|
erase_char = term_vi.c_cc[VERASE];
|
|
|
|
tcsetattr(0, TCSANOW, &term_vi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cookmode(void)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
tcsetattr(0, TCSANOW, &term_orig);
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:04 +00:00
|
|
|
void linux_putc(const char c)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
|
|
|
fputc(c, stdout);
|
|
|
|
|
|
|
|
/* If \n, also do \r */
|
|
|
|
if (c == '\n')
|
2007-07-05 16:01:51 +00:00
|
|
|
linux_putc ('\r');
|
|
|
|
|
|
|
|
fflush(stdout);
|
2007-07-05 16:01:42 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:04 +00:00
|
|
|
int linux_tstc(int fd)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
2007-11-28 09:05:47 +00:00
|
|
|
struct timeval tv = {
|
|
|
|
.tv_usec = 100,
|
|
|
|
};
|
2007-07-05 16:01:51 +00:00
|
|
|
fd_set rfds;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
2007-07-05 16:01:59 +00:00
|
|
|
FD_SET(fd, &rfds);
|
2007-07-05 16:01:51 +00:00
|
|
|
|
2007-07-05 16:02:18 +00:00
|
|
|
/*
|
|
|
|
* We set the timeout here to 100us, because otherwise
|
|
|
|
* U-Boot would eat all cpu resources while waiting
|
|
|
|
* for input. On the other hand this makes some
|
|
|
|
* things like networking slow, because U-Boot will
|
|
|
|
* poll this function very often.
|
|
|
|
*/
|
2007-07-05 16:01:59 +00:00
|
|
|
ret = select(fd + 1, &rfds, NULL, NULL, &tv);
|
2007-07-05 16:01:51 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
|
|
|
|
2007-07-05 16:01:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:04 +00:00
|
|
|
int linux_getc(void)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
2007-07-05 16:01:51 +00:00
|
|
|
char ret;
|
|
|
|
|
|
|
|
read(0, &ret, 1);
|
|
|
|
|
|
|
|
return ret;
|
2007-07-05 16:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t linux_get_time(void)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
uint64_t now;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
|
|
|
|
now = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
|
|
|
|
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:18 +00:00
|
|
|
int reset_cpu(int unused)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
|
|
|
cookmode();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:04 +00:00
|
|
|
void enable_interrupts(void)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:04 +00:00
|
|
|
void disable_interrupt(void)
|
2007-07-05 16:01:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:42 +00:00
|
|
|
int linux_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
2007-11-28 09:12:38 +00:00
|
|
|
ssize_t ret;
|
|
|
|
|
2007-12-23 19:07:03 +00:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-11-28 09:12:38 +00:00
|
|
|
do {
|
|
|
|
ret = read(fd, buf, count);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
printf("read on fd %d returned 0, device gone? - exiting\n", fd);
|
|
|
|
reset_cpu(0);
|
|
|
|
} else if (ret == -1) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
usleep(100);
|
|
|
|
else if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else {
|
|
|
|
printf("read on fd %d returned -1, errno %d - exiting\n", fd, errno);
|
|
|
|
reset_cpu(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (ret <= 0);
|
|
|
|
|
|
|
|
return (int)ret;
|
2007-07-05 16:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int linux_read_nonblock(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
int oldflags, ret;
|
|
|
|
|
|
|
|
oldflags = fcntl(fd, F_GETFL);
|
|
|
|
if (oldflags == -1)
|
|
|
|
goto err_out;
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, oldflags | O_NONBLOCK) == -1)
|
|
|
|
goto err_out;
|
|
|
|
|
2007-11-28 09:12:38 +00:00
|
|
|
ret = linux_read(fd, buf, count);
|
2007-07-05 16:01:42 +00:00
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, oldflags) == -1)
|
|
|
|
goto err_out;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
perror("fcntl");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t linux_write(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
return write(fd, buf, count);
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:59 +00:00
|
|
|
off_t linux_lseek(int fd, off_t offset)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
2007-07-05 16:01:59 +00:00
|
|
|
return lseek(fd, offset, SEEK_SET);
|
2007-07-05 16:01:42 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:04 +00:00
|
|
|
void flush_cache(unsigned long dummy1, unsigned long dummy2)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
|
|
|
/* why should we? */
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void start_uboot(void);
|
2007-07-05 16:02:04 +00:00
|
|
|
extern void mem_malloc_init(void *start, void *end);
|
2007-07-05 16:01:42 +00:00
|
|
|
|
2007-11-15 22:55:47 +00:00
|
|
|
static int add_image(char *str, char *name_template)
|
2007-07-05 16:01:42 +00:00
|
|
|
{
|
|
|
|
char *file;
|
|
|
|
int readonly = 0, map = 1;
|
|
|
|
struct stat s;
|
|
|
|
char *opt;
|
|
|
|
int fd, ret;
|
|
|
|
struct hf_platform_data *hf = malloc(sizeof(struct hf_platform_data));
|
|
|
|
|
|
|
|
if (!hf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
file = strtok(str, ",");
|
|
|
|
while ((opt = strtok(NULL, ","))) {
|
|
|
|
if (!strcmp(opt, "ro"))
|
|
|
|
readonly = 1;
|
|
|
|
if (!strcmp(opt, "map"))
|
|
|
|
map = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("add file %s(%s)\n", file, readonly ? "ro" : "");
|
|
|
|
|
|
|
|
fd = open(file, readonly ? O_RDONLY : O_RDWR);
|
|
|
|
hf->fd = fd;
|
|
|
|
hf->filename = file;
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &s)) {
|
|
|
|
perror("fstat");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
hf->size = s.st_size;
|
|
|
|
|
|
|
|
if (map) {
|
2007-11-15 22:55:47 +00:00
|
|
|
hf->map_base = (unsigned long)mmap(NULL, hf->size,
|
2007-07-05 16:01:42 +00:00
|
|
|
PROT_READ | (readonly ? 0 : PROT_WRITE),
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if ((void *)hf->map_base == MAP_FAILED)
|
|
|
|
printf("warning: mmapping %s failed\n", file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ret = u_boot_register_filedev(hf, name_template);
|
|
|
|
if (ret)
|
|
|
|
goto err_out;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
if (fd > 0)
|
|
|
|
close(fd);
|
|
|
|
free(hf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-20 14:56:29 +00:00
|
|
|
static void print_usage(const char*);
|
2007-07-05 16:01:42 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
void *ram;
|
2007-07-05 16:01:59 +00:00
|
|
|
int opt, ret, fd;
|
2007-07-05 16:02:18 +00:00
|
|
|
int malloc_size = 8 * 1024 * 1024;
|
2007-07-05 16:01:42 +00:00
|
|
|
|
|
|
|
ram = malloc(malloc_size);
|
|
|
|
if (!ram) {
|
|
|
|
printf("unable to get malloc space\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
mem_malloc_init(ram, ram + malloc_size);
|
|
|
|
|
2007-07-05 16:01:59 +00:00
|
|
|
while ((opt = getopt(argc, argv, "hi:e:I:O:")) != -1) {
|
2007-07-05 16:01:42 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'h':
|
|
|
|
print_usage(basename(argv[0]));
|
|
|
|
exit(0);
|
|
|
|
case 'i':
|
|
|
|
ret = add_image(optarg, "fd");
|
|
|
|
if (ret)
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
case 'm':
|
2007-07-05 16:01:57 +00:00
|
|
|
/* This option is broken. add_image needs malloc, so
|
|
|
|
* mem_alloc_init() has to be called before option
|
|
|
|
* parsing
|
|
|
|
*/
|
2007-07-05 16:01:42 +00:00
|
|
|
malloc_size = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
ret = add_image(optarg, "env");
|
|
|
|
if (ret)
|
|
|
|
exit(1);
|
|
|
|
break;
|
2007-07-05 16:01:59 +00:00
|
|
|
case 'O':
|
|
|
|
fd = open(optarg, O_WRONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_boot_register_console("cout", -1, fd);
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
fd = open(optarg, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_boot_register_console("cin", fd, -1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(1);
|
2007-07-05 16:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-15 21:19:08 +00:00
|
|
|
u_boot_register_console("console", fileno(stdin), fileno(stdout));
|
2007-07-05 16:01:59 +00:00
|
|
|
|
2007-07-05 16:01:42 +00:00
|
|
|
rawmode();
|
|
|
|
start_uboot();
|
|
|
|
|
|
|
|
/* never reached */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-15 20:40:11 +00:00
|
|
|
#ifdef __PPC__
|
|
|
|
/* HACK: we need this symbol on PPC, better ask a PPC export about this :) */
|
|
|
|
char _SDA_BASE_[4096];
|
|
|
|
#endif
|
|
|
|
|
2007-10-20 14:56:29 +00:00
|
|
|
static void print_usage(const char *prgname)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Usage: %s [OPTIONS]\n"
|
|
|
|
"Start U-Boot.\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -i <file> Map a file to U-Boot. This option can be given multiple\n"
|
|
|
|
" times. The files will show up as /dev/fd0 ... /dev/fdx\n"
|
|
|
|
" under U-Boot.\n"
|
|
|
|
" -e <file> Map a file to U-Boot. With this option files are mapped as\n"
|
|
|
|
" /dev/env0 ... /dev/envx and thus are used as default\n"
|
|
|
|
" environment. An empty file generated with dd will do to get\n"
|
|
|
|
" started wth an empty environment\n"
|
|
|
|
" -O <file> Register file as a console capable of doing stdout. File can\n"
|
|
|
|
" be a regular file or a fifo.\n"
|
|
|
|
" -I <file> Register file as a console capable of doing stdin. File can\n"
|
|
|
|
" be a regular file or a fifo.\n",
|
|
|
|
prgname
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @page uboot_simul U-Boot Simulator
|
|
|
|
*
|
|
|
|
* U-Boot can be run as a simulator on your host to check and debug new non
|
|
|
|
* hardware related features.
|
|
|
|
*
|
|
|
|
* @section simu_build How to build U-Boot for simulation
|
|
|
|
*
|
|
|
|
* @section simu_run How to run U-Boot simulator
|
|
|
|
*
|
|
|
|
* $ uboot [<OPTIONS>]
|
|
|
|
*
|
|
|
|
* Options can be:
|
|
|
|
*
|
|
|
|
* -i <file>
|
|
|
|
*
|
|
|
|
* Map a <file> to U-Boot. This option can be given multiple times. The <file>s
|
|
|
|
* will show up as /dev/fd0 ... /dev/fdx in the U-Boot simulator.
|
|
|
|
*
|
|
|
|
* -e <file>
|
|
|
|
*
|
|
|
|
* Map <file> to U-Boot. With this option <file>s are mapped as /dev/env0 ...
|
|
|
|
* /dev/envx and thus are used as default environment. A clean file generated
|
|
|
|
* with dd will do to get started with an empty environment
|
|
|
|
*
|
|
|
|
* -O <file>
|
|
|
|
*
|
|
|
|
* Register <file> as a console capable of doing stdout. <file> can be a
|
|
|
|
* regular file or a fifo.
|
|
|
|
*
|
|
|
|
* -I <file>
|
|
|
|
*
|
|
|
|
* Register <file> as a console capable of doing stdin. <file> can be a regular
|
|
|
|
* file or a fifo.
|
|
|
|
*
|
|
|
|
* @section simu_dbg How to debug U-Boot simulator
|
|
|
|
*
|
2007-11-28 09:05:47 +00:00
|
|
|
*/
|