Skip to content
Snippets Groups Projects
Commit 67fa0f89 authored by Benjamin Larsson's avatar Benjamin Larsson
Browse files

Webserver: initial commit

parent 6cd538fb
Branches airoha_bl
No related tags found
No related merge requests found
Pipeline #56535 failed
Showing
with 1313 additions and 0 deletions
config BCMBCA_HTTPD
bool "Support HTTPD server"
depends on NET
# SPDX-License-Identifier: GPL-2.0
# Empty file to make sure compilation pass
obj-y += empty.o
obj-$(CONFIG_BCMBCA_HTTPD) += httpd/
obj-$(CONFIG_BCMBCA_HTTPD) += cmd_ihttpd.o bca_common.o
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Generic autoboot menu helper
*/
#ifndef _AUTOBOOT_HELPER_H_
#define _AUTOBOOT_HELPER_H_
#include <linux/types.h>
#include <stdbool.h>
struct bootmenu_entry {
const char *desc;
const char *cmd;
};
extern void board_bootmenu_entries(const struct bootmenu_entry **menu,
u32 *count);
#endif /* _AUTOBOOT_HELPER_H_ */
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2019 Broadcom Ltd.
*/
#include <common.h>
#include <linux/ctype.h>
#include <stdlib.h>
#include <string.h>
#include <mmc.h>
#include <mtd.h>
#include <spl.h>
#include "bca_common.h"
#define CLI_CB_NUM 8
struct cli_job_cb
{
unsigned long last_time;
unsigned long time_period;
void (*job_cb)(void);
};
static unsigned long registred_cb_count = 0;
static struct cli_job_cb cli_job_cb_arr[CLI_CB_NUM];
void init_cli_cb_arr(void)
{
memset(cli_job_cb_arr, 0, sizeof(struct cli_job_cb)*CLI_CB_NUM);
}
void register_cli_job_cb(unsigned long time_period, void (*job_cb)(void))
{
int i;
for(i=0; i<CLI_CB_NUM; i++)
{
if (!cli_job_cb_arr[i].job_cb)
{
cli_job_cb_arr[i].job_cb = job_cb;
cli_job_cb_arr[i].time_period = time_period;
cli_job_cb_arr[i].last_time = get_timer(0);
registred_cb_count++;
break;
}
}
}
void unregister_cli_job_cb(void (*job_cb)(void))
{
int i,j;
for(i=0; i<registred_cb_count; i++)
{
if (cli_job_cb_arr[i].job_cb == job_cb)
{
for(j=i; j<registred_cb_count; j++)
{
if(j+1 == registred_cb_count)
{
memset(&cli_job_cb_arr[j], 0, sizeof(struct cli_job_cb));
break;
}
memcpy(&cli_job_cb_arr[j], &cli_job_cb_arr[j+1], sizeof(struct cli_job_cb));
}
registred_cb_count--;
break;
}
}
}
void run_cli_jobs(void)
{
int i;
for(i=0; i<registred_cb_count; i++)
{
if(cli_job_cb_arr[i].job_cb)
{
if(!cli_job_cb_arr[i].time_period)
{
cli_job_cb_arr[i].job_cb();
}
else
{
if(get_timer(cli_job_cb_arr[i].last_time) >= cli_job_cb_arr[i].time_period)
{
cli_job_cb_arr[i].job_cb();
cli_job_cb_arr[i].last_time = get_timer(0);
}
}
}
}
}
/* SPDX-License-Identifier: GPL-2.0+
*
* Copyright 2019 Broadcom Ltd.
*/
#ifndef _BCMBCA_COMMON_H
#define _BCMBCA_COMMON_H
void init_cli_cb_arr(void);
void register_cli_job_cb(unsigned long time_period, void (*job_cb)(void));
void unregister_cli_job_cb(void (*job_cb)(void));
void run_cli_jobs(void);
#endif
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Generic image boot helper
*/
#include <errno.h>
#include <image.h>
#include <mmc.h>
#include <part.h>
#include <part_efi.h>
#include <linux/types.h>
#include <linux/mtd/mtd.h>
#include <linux/sizes.h>
#include "upgrade_helper.h"
int boot_from_mem(ulong data_load_addr)
{
char cmd[64];
#ifdef CONFIG_MTK_SECURE_BOOT
int ret;
/* boot processes for secure boot */
/* verify FIT image */
snprintf(cmd, sizeof(cmd),
"bootm start 0x%lx#config-1", data_load_addr);
ret = run_command(cmd, 0);
if (ret)
return ret;
/* run u-boot script to get dm-verity information */
snprintf(cmd, sizeof(cmd), "source 0x%lx:script-1", data_load_addr);
ret = run_command(cmd, 0);
if (ret)
return ret;
/* using config-1 to boot */
snprintf(cmd, sizeof(cmd), "bootm 0x%lx#config-1", data_load_addr);
#else
/* boot processes for normal boot */
snprintf(cmd, sizeof(cmd), "bootm 0x%lx", data_load_addr);
#endif
return run_command(cmd, 0);
}
#ifdef CONFIG_MMC
static int _boot_from_mmc(u32 dev, struct mmc *mmc, u64 offset)
{
ulong data_load_addr;
u32 size;
int ret;
/* Set load address */
#if defined(CONFIG_SYS_LOAD_ADDR)
data_load_addr = CONFIG_SYS_LOAD_ADDR;
#elif defined(CONFIG_LOADADDR)
data_load_addr = CONFIG_LOADADDR;
#endif
ret = _mmc_read(mmc, offset, (void *)data_load_addr, mmc->read_bl_len);
if (ret)
return ret;
switch (genimg_get_format((const void *)data_load_addr)) {
#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
case IMAGE_FORMAT_LEGACY:
size = image_get_image_size((const void *)data_load_addr);
break;
#endif
#if defined(CONFIG_FIT)
case IMAGE_FORMAT_FIT:
size = fit_get_size((const void *)data_load_addr);
break;
#endif
default:
printf("Error: no Image found in MMC device %u at 0x%llx\n",
dev, offset);
return -EINVAL;
}
ret = _mmc_read(mmc, offset, (void *)data_load_addr, size);
if (ret)
return ret;
return boot_from_mem(data_load_addr);
}
int boot_from_mmc_generic(u32 dev, int part, u64 offset)
{
struct mmc *mmc;
mmc = _mmc_get_dev(dev, part, false);
if (!mmc)
return -ENODEV;
return _boot_from_mmc(dev, mmc, offset);
}
#ifdef CONFIG_PARTITIONS
int boot_from_mmc_partition(u32 dev, int part, const char *part_name)
{
struct disk_partition dpart;
struct mmc *mmc;
int ret;
mmc = _mmc_get_dev(dev, part, false);
if (!mmc)
return -ENODEV;
ret = _mmc_find_part(mmc, part_name, &dpart);
if (ret)
return ret;
return _boot_from_mmc(dev, mmc, (u64)dpart.start * dpart.blksz);
}
#endif /* CONFIG_PARTITIONS */
#endif /* CONFIG_GENERIC_MMC */
#ifdef CONFIG_MTD
int boot_from_mtd(struct mtd_info *mtd, u64 offset)
{
ulong data_load_addr;
u32 size;
int ret;
/* Set load address */
#if defined(CONFIG_SYS_LOAD_ADDR)
data_load_addr = CONFIG_SYS_LOAD_ADDR;
#elif defined(CONFIG_LOADADDR)
data_load_addr = CONFIG_LOADADDR;
#endif
ret = mtd_read_generic(mtd, offset, (void *)data_load_addr,
mtd->writesize);
if (ret)
return ret;
switch (genimg_get_format((const void *)data_load_addr)) {
#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
case IMAGE_FORMAT_LEGACY:
size = image_get_image_size((const void *)data_load_addr);
break;
#endif
#if defined(CONFIG_FIT)
case IMAGE_FORMAT_FIT:
size = fit_get_size((const void *)data_load_addr);
break;
#endif
default:
printf("Error: no Image found in %s at 0x%llx\n", mtd->name,
offset);
return -EINVAL;
}
ret = mtd_read_generic(mtd, offset, (void *)data_load_addr, size);
if (ret)
return ret;
return boot_from_mem(data_load_addr);
}
#endif
#ifdef CONFIG_DM_SPI_FLASH
int boot_from_snor(struct spi_flash *snor, u32 offset)
{
ulong data_load_addr;
u32 size;
int ret;
/* Set load address */
#if defined(CONFIG_SYS_LOAD_ADDR)
data_load_addr = CONFIG_SYS_LOAD_ADDR;
#elif defined(CONFIG_LOADADDR)
data_load_addr = CONFIG_LOADADDR;
#endif
ret = snor_read_generic(snor, offset, (void *)data_load_addr,
snor->page_size);
if (ret)
return ret;
switch (genimg_get_format((const void *)data_load_addr)) {
#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
case IMAGE_FORMAT_LEGACY:
size = image_get_image_size((const void *)data_load_addr);
break;
#endif
#if defined(CONFIG_FIT)
case IMAGE_FORMAT_FIT:
size = fit_get_size((const void *)data_load_addr);
break;
#endif
default:
printf("Error: no Image found in SPI-NOR at 0x%x\n", offset);
return -EINVAL;
}
ret = snor_read_generic(snor, offset, (void *)data_load_addr, size);
if (ret)
return ret;
return boot_from_mem(data_load_addr);
}
#endif
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Generic image boot helper
*/
#ifndef _BOOT_HELPER_H_
#define _BOOT_HELPER_H_
#include <linux/types.h>
extern int board_boot_default(void);
int boot_from_mem(ulong data_load_addr);
#ifdef CONFIG_MMC
int boot_from_mmc_generic(u32 dev, int part, u64 offset);
#ifdef CONFIG_PARTITIONS
int boot_from_mmc_partition(u32 dev, int part, const char *part_name);
#endif /* CONFIG_PARTITIONS */
#endif /* CONFIG_GENERIC_MMC */
#ifdef CONFIG_MTD
#include <linux/mtd/mtd.h>
int boot_from_mtd(struct mtd_info *mtd, u64 offset);
#endif
#ifdef CONFIG_DM_SPI_FLASH
#include <spi_flash.h>
int boot_from_snor(struct spi_flash *snor, u32 offset);
#endif
#endif /* _BOOT_HELPER_H_ */
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2022 Genexis AB All Rights Reserved.
*
* Author: Benjamin Larsson <benjamin.larsson@genexis.eu>
*
* Command to start the httpd server
*/
#include <command.h>
#include <linux/types.h>
#include "httpd/bcmbca_net.h"
#ifdef CONFIG_BCMBCA_HTTPD
static int do_start_ihttpd(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
if(!httpd_check_net_env())
register_cli_job_cb(0, http_poll);
return CMD_RET_SUCCESS;
}
U_BOOT_CMD(start_ihttpd, 1, 0, do_start_ihttpd,
"Start httpd server", ""
);
#endif
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Generic autoboot menu command
*/
#include <command.h>
#include <env.h>
#include <stdio.h>
#include <vsprintf.h>
#include <linux/types.h>
#include "autoboot_helper.h"
static const struct bootmenu_entry *menu_entries;
static u32 menu_count;
static int do_mtkautoboot(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
int i;
char key[16];
char val[256];
char cmd[32];
int delay = -1;
#ifdef CONFIG_MEDIATEK_BOOTMENU_COUNTDOWN
const char *delay_str;
#endif
board_bootmenu_entries(&menu_entries, &menu_count);
if (!menu_entries || !menu_count) {
printf("mtkautoboot is not configured!\n");
return CMD_RET_FAILURE;
}
for (i = 0; i < menu_count; i++) {
snprintf(key, sizeof(key), "bootmenu_%d", i);
snprintf(val, sizeof(val), "%s=%s",
menu_entries[i].desc, menu_entries[i].cmd);
env_set(key, val);
}
/*
* Remove possibly existed `next entry` to force bootmenu command to
* stop processing
*/
snprintf(key, sizeof(key), "bootmenu_%d", i);
env_set(key, NULL);
#ifdef CONFIG_MEDIATEK_BOOTMENU_COUNTDOWN
delay_str = env_get("bootmenu_delay");
if (delay_str)
delay = simple_strtol(delay_str, NULL, 10);
else
delay = CONFIG_MEDIATEK_BOOTMENU_DELAY;
#endif
snprintf(cmd, sizeof(cmd), "bootmenu %d", delay);
run_command(cmd, 0);
return CMD_RET_SUCCESS;
}
U_BOOT_CMD(mtkautoboot, 1, 0, do_mtkautoboot,
"Display MediaTek bootmenu", ""
);
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Generic image booting command
*/
#include <command.h>
#include <linux/types.h>
#include "boot_helper.h"
static int do_mtkboardboot(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
int ret;
ret = board_boot_default();
if (ret)
return CMD_RET_FAILURE;
return CMD_RET_SUCCESS;
}
U_BOOT_CMD(mtkboardboot, 1, 0, do_mtkboardboot,
"Boot MTK firmware", ""
);
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Generic data loading command
*/
#include <command.h>
#include <env.h>
#include <image.h>
#include <vsprintf.h>
#include <linux/stringify.h>
#include <linux/types.h>
#include "load_data.h"
#include "colored_print.h"
static int run_image(ulong data_addr, size_t data_size)
{
char *argv[2], str[64];
sprintf(str, "0x%lx", data_addr);
argv[0] = "bootm";
argv[1] = str;
return do_bootm(find_cmd("do_bootm"), 0, 2, argv);
}
static int do_mtkload(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
char s_load_addr[CONFIG_SYS_CBSIZE + 1];
char *addr_end, *def_load_addr = NULL;
ulong data_load_addr;
size_t data_size = 0;
printf("\n");
cprintln(PROMPT, "*** Loading image ***");
printf("\n");
/* Set load address */
#if defined(CONFIG_SYS_LOAD_ADDR)
def_load_addr = __stringify(CONFIG_SYS_LOAD_ADDR);
#elif defined(CONFIG_LOADADDR)
def_load_addr = __stringify(CONFIG_LOADADDR);
#endif
if (env_update("loadaddr", def_load_addr, "Input load address:",
s_load_addr, sizeof(s_load_addr)))
return CMD_RET_FAILURE;
data_load_addr = simple_strtoul(s_load_addr, &addr_end, 0);
if (*addr_end) {
printf("\n");
cprintln(ERROR, "*** Invalid load address! ***");
return CMD_RET_FAILURE;
}
printf("\n");
/* Load data */
if (load_data(data_load_addr, &data_size, "bootfile"))
return CMD_RET_FAILURE;
printf("\n");
cprintln(PROMPT, "*** Loaded %zd (0x%zx) bytes at 0x%08lx ***",
data_size, data_size, data_load_addr);
printf("\n");
image_load_addr = data_load_addr;
/* Whether to run or not */
if (confirm_yes("Run loaded data now? (Y/n):")) {
/* Run image */
return run_image(data_load_addr, data_size);
}
return CMD_RET_SUCCESS;
}
U_BOOT_CMD(mtkload, 1, 0, do_mtkload, "MTK image loading utility", NULL);
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Generic data upgrading command
*/
#include <command.h>
#include <env.h>
#include <image.h>
#include <linux/types.h>
#include "load_data.h"
#include "colored_print.h"
#include "upgrade_helper.h"
static const struct data_part_entry *upgrade_parts;
static u32 num_parts;
static bool prompt_post_action(const struct data_part_entry *dpe)
{
if (dpe->post_action == UPGRADE_ACTION_REBOOT)
return confirm_yes("Reboot after upgrading? (Y/n):");
if (dpe->post_action == UPGRADE_ACTION_BOOT)
return confirm_yes("Run image after upgrading? (Y/n):");
if (dpe->post_action == UPGRADE_ACTION_CUSTOM) {
if (dpe->custom_action_prompt)
return confirm_yes(dpe->custom_action_prompt);
return true;
}
return false;
}
static int do_post_action(const struct data_part_entry *dpe, const void *data,
size_t size)
{
int ret;
if (dpe->do_post_action) {
ret = dpe->do_post_action(dpe->priv, dpe, data, size);
if (dpe->post_action == UPGRADE_ACTION_CUSTOM)
return ret;
}
if (dpe->post_action == UPGRADE_ACTION_REBOOT) {
printf("Rebooting ...\n\n");
return run_command("reset", 0);
}
if (dpe->post_action == UPGRADE_ACTION_BOOT)
return run_command("mtkboardboot", 0);
return CMD_RET_SUCCESS;
}
static const struct data_part_entry *select_part(void)
{
u32 i;
char c;
printf("\n");
cprintln(PROMPT, "Available parts to be upgraded:");
for (i = 0; i < num_parts; i++)
printf(" %d - %s\n", i, upgrade_parts[i].name);
while (1) {
printf("\n");
cprint(PROMPT, "Select a part:");
printf(" ");
c = getchar();
if (c == '\r' || c == '\n')
continue;
printf("%c\n", c);
break;
}
i = c - '0';
if (c < '0' || i >= num_parts) {
cprintln(ERROR, "*** Invalid selection! ***");
return NULL;
}
return &upgrade_parts[i];
}
static const struct data_part_entry *find_part(const char *abbr)
{
u32 i;
if (!abbr)
return NULL;
for (i = 0; i < num_parts; i++) {
if (!strcmp(upgrade_parts[i].abbr, abbr))
return &upgrade_parts[i];
}
cprintln(ERROR, "*** Invalid upgrading part! ***");
return NULL;
}
static int do_mtkupgrade(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
const struct data_part_entry *dpe = NULL;
ulong data_load_addr;
size_t data_size = 0;
bool do_action;
board_upgrade_data_parts(&upgrade_parts, &num_parts);
if (!upgrade_parts || !num_parts) {
printf("mtkupgrade is not configured!\n");
return CMD_RET_FAILURE;
}
if (argc < 2)
dpe = select_part();
else
dpe = find_part(argv[1]);
if (!dpe)
return CMD_RET_FAILURE;
printf("\n");
cprintln(PROMPT, "*** Upgrading %s ***", dpe->name);
printf("\n");
do_action = prompt_post_action(dpe);
/* Set load address */
#if defined(CONFIG_SYS_LOAD_ADDR)
data_load_addr = CONFIG_SYS_LOAD_ADDR;
#elif defined(CONFIG_LOADADDR)
data_load_addr = CONFIG_LOADADDR;
#endif
/* Load data */
if (load_data(data_load_addr, &data_size, dpe->env_name))
return CMD_RET_FAILURE;
printf("\n");
cprintln(PROMPT, "*** Loaded %zd (0x%zx) bytes at 0x%08lx ***",
data_size, data_size, data_load_addr);
printf("\n");
image_load_addr = data_load_addr;
/* Validate data */
if (dpe->validate) {
if (dpe->validate(dpe->priv, dpe, (void *)data_load_addr,
data_size))
return CMD_RET_FAILURE;
}
/* Write data */
if (dpe->write(dpe->priv, dpe, (void *)data_load_addr, data_size))
return CMD_RET_FAILURE;
printf("\n");
cprintln(PROMPT, "*** %s upgrade completed! ***", dpe->name);
if (do_action) {
puts("\n");
return do_post_action(dpe, (void *)data_load_addr, data_size);
}
return CMD_RET_SUCCESS;
}
U_BOOT_CMD(mtkupgrade, 2, 0, do_mtkupgrade,
"MTK firmware/bootloader upgrading utility",
"mtkupgrade [<part>]\n"
"part - upgrade data part\n"
);
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
*
* Author: Weijie Gao <weijie.gao@mediatek.com>
*
* Helper to print colored texts
*/
#ifndef _COLORED_PRINT_H_
#define _COLORED_PRINT_H_
#include <stdio.h>
#define COLOR_PROMPT "\x1b[0;33m"
#define COLOR_INPUT "\x1b[4;36m"
#define COLOR_ERROR "\x1b[93;41m"
#define COLOR_CAUTION "\x1b[1;31m"
#define COLOR_NORMAL "\x1b[0m"
#define cprintln(color, fmt, ...) \
printf(COLOR_##color fmt COLOR_NORMAL "\n", ##__VA_ARGS__)
#define cprint(color, fmt, ...) \
printf(COLOR_##color fmt COLOR_NORMAL, ##__VA_ARGS__)
#define cprint_cont(color, fmt, ...) \
printf(COLOR_##color fmt, ##__VA_ARGS__)
#endif /* _COLORED_PRINT_H_ */
# SPDX-License-Identifier: GPL-2.0+
#
# (C) Copyright 2019 Broadcom Ltd
#
# ccflags-y += -save-temps
obj-$(CONFIG_BCMBCA_HTTPD) += bcmbca_net.o
obj-$(CONFIG_BCMBCA_HTTPD) += httpd.o
obj-$(CONFIG_BCMBCA_HTTPD) += uip.o
obj-$(CONFIG_BCMBCA_HTTPD) += uip_arch.o
obj-$(CONFIG_BCMBCA_HTTPD) += uip_arp.o
#include <common.h>
#include <command.h>
#include <console.h>
#include <errno.h>
#include <net.h>
#include "uipopt.h"
#include "uip.h"
#include "uip_arp.h"
#include "httpd.h"
//#include "bca_sdk.h"
#include "../bca_common.h"
typedef enum
{
HTTP_IMG_UPGRADE_IDLE,
HTTP_IMG_UPGRADE_INPROGRESS,
HTTP_IMG_UPGRADE_OK,
HTTP_IMG_UPGRADE_WAIT_RESET,
HTTP_IMG_UPGRADE_FAIL,
} HTTP_IMG_UPGRADE_STATE;
static HTTP_IMG_UPGRADE_STATE http_upgrade_state = HTTP_IMG_UPGRADE_IDLE;
static uint64_t httpd_reset_wait_start_ticks = 0;
static uint64_t httpd_reset_wait_timeout_sec = 0;
extern int (*ip_tap)(uchar *in_packet, int len, struct ip_udp_hdr *ip);
#define HTTPD_RESET_WAIT_SEC 3 /* Amount of seconds to wait before triggering device reset */
#define IPPROTO_TCP 6 /* Trasmission Control Protocol */
extern void (*jobs_func)(void);
void send_httpd(void)
{
volatile uchar *tmpbuf;
uchar *tx_packet;
int i;
tx_packet = net_get_async_tx_pkt_buf();
tmpbuf = tx_packet;
for(i = 0; i < 40 + UIP_LLH_LEN; i++)
tmpbuf[i] = uip_buf[i];
for(; i < uip_len; i++)
tmpbuf[i] = uip_appdata[i - 40 - UIP_LLH_LEN];
net_send_packet(tx_packet, uip_len);
}
int httpd_check_net_env(void)
{
char eth_addr[6];
if(!env_get("ipaddr"))
{
printf("HTTPD: ipaddr not defined\n");
return -1;
}
if(!env_get("netmask"))
{
printf("HTTPD: netmask not defined\n");
return -1;
}
if(!eth_env_get_enetaddr("ethaddr",eth_addr))
{
printf("HTTPD: ethaddr not defined\n");
return -1;
}
printf("HTTPD: ready for starting\n");
return 0;
}
void httpd_start(void)
{
struct uip_eth_addr eaddr;
unsigned short ip[2];
char eth_addr[6];
char *ip_addr = NULL, *net_mask = NULL;
uip_init();
ip_addr = env_get("ipaddr");
if(ip_addr)
{
net_ip = string_to_ip(ip_addr);
}
else
{
printf("httpd_start: ipaddr not defined\n");
return;
}
net_mask = env_get("netmask");
if(net_mask)
{
net_netmask = string_to_ip(net_mask);
}
else
{
printf("httpd_start: netmask not defined\n");
return;
}
ip[0] = net_ip.s_addr & 0x0000FFFF;
ip[1] = (net_ip.s_addr & 0xFFFF0000) >> 16;
uip_sethostaddr(ip);
ip[0] = net_netmask.s_addr & 0x0000FFFF;
ip[1] = (net_netmask.s_addr & 0xFFFF0000) >> 16;
uip_setnetmask(ip);
if(eth_env_get_enetaddr("ethaddr",eth_addr))
{
memcpy(net_ethaddr, eth_addr, 6);
}
else
{
printf("httpd_start: ethaddr not defined\n");
return;
}
memcpy(eaddr.addr, net_ethaddr, 6);
uip_setethaddr(eaddr);
uip_listen(HTONS(80));
}
int httpd_poll_post_process(void)
{
/* Handle device reset after succesfull image upgrade */
if( http_upgrade_state == HTTP_IMG_UPGRADE_WAIT_RESET )
{
if( get_ticks() - httpd_reset_wait_start_ticks > httpd_reset_wait_timeout_sec * get_tbclk())
do_reset(NULL, 0, 0, NULL);
}
}
int http_rcv(uchar *in_packet, int len, struct ip_udp_hdr *ip)
{
if(ip->ip_p == IPPROTO_TCP)
{
if(len > sizeof(uip_buf))
{
printf("TCP/IP packet len %d > uip buffer %d\n", len, sizeof(uip_buf));
return 1;
}
memcpy(uip_buf, in_packet, len);
uip_arp_ipin();
uip_input();
if(uip_len > 0)
{
uip_arp_out();
send_httpd();
}
if( uip_aborted() && (http_upgrade_state != HTTP_IMG_UPGRADE_WAIT_RESET) )
http_upgrade_state = HTTP_IMG_UPGRADE_IDLE;
/* If upgrade succeeded, reset after sending confirmation page */
if( http_upgrade_state > HTTP_IMG_UPGRADE_INPROGRESS )
{
/* Issue reset after sending update page if upgrade was good */
switch(http_upgrade_state)
{
case HTTP_IMG_UPGRADE_OK:
/* Prepare to close TCP connection */
uip_close();
/* Set state to waiting for reset */
http_upgrade_state = HTTP_IMG_UPGRADE_WAIT_RESET;
/* Arm the reset counter */
httpd_reset_wait_start_ticks = get_ticks();
httpd_reset_wait_timeout_sec = HTTPD_RESET_WAIT_SEC;
printk("Resetting in %llu seconds....\n", httpd_reset_wait_timeout_sec);
break;
case HTTP_IMG_UPGRADE_WAIT_RESET:
/* Do nothing */
break;
default:
http_upgrade_state = HTTP_IMG_UPGRADE_IDLE;
break;
}
}
return 1;
}
return 0;
}
STREAM_UPGRADE_STATUS http_get_upgrade_status(void)
{
if( http_upgrade_state == HTTP_IMG_UPGRADE_FAIL )
return STREAM_UPGRADE_ERR;
else
return STREAM_UPGRADE_OK;
}
extern ulong load_addr; /* Default Load Address */
extern ulong save_addr; /* Default Save Address */
extern ulong save_size; /* Default Save Size */
STREAM_TRANSFER_STATUS http_update_image(char *data, unsigned int len, STREAM_TRANSFER_STATE state)
{
static char* upload_addr = NULL;
static char* orig_upload_addr = NULL;
char * s = NULL;
int ret = -1;
if( state == TRANSFER_START ) {
if( http_upgrade_state == HTTP_IMG_UPGRADE_IDLE ) {
/* pre-set load_addr */
s = env_get("loadaddr");
if (s != NULL)
upload_addr = simple_strtoul(s, NULL, 16);
// else
// upload_addr = load_addr;
orig_upload_addr = upload_addr;
http_upgrade_state = HTTP_IMG_UPGRADE_INPROGRESS;
printf("%s: downloading image to 0x%p\n ", __FUNCTION__, orig_upload_addr);
} else {
printf("%s: ERROR: Image Upgrade already in progress! Ignoring Upgrade request!p\n", __FUNCTION__);
return STOP_STREAM_DATA;
}
}
if( upload_addr ) {
memcpy(upload_addr, data, len);
upload_addr += len;
}
#if 0
if( state == TRANSFER_END )
{
int img_index = get_img_index_for_upgrade(0);
ret = flash_upgrade_img_bundle(orig_upload_addr, img_index, NULL);
upload_addr = NULL;
orig_upload_addr = NULL;
if( ret ) {
extern void sk9822_set_color_error(void);
printf("ERROR: HTTP Image upgrade failed!!\n");
http_upgrade_state = HTTP_IMG_UPGRADE_FAIL;
sk9822_set_color_error();
} else {
printf("INFO: HTTP Image upgrade successfull!!\n");
//FIXME: Check boot once flag before committing?
commit_image( img_index );
/* iopsys: we need to tell the runnig system there has been a new installation */
env_set("iopsys_upgrade", "u-boot");
env_save();
http_upgrade_state = HTTP_IMG_UPGRADE_OK;
}
}
#endif
return NEXT_STREAM_DATA;
}
void http_poll(void)
{
int ret;
struct udevice *current;
static int uip_initalized = 0;
current = eth_get_dev();
if(!current || !eth_is_active(current))
{
net_init();
if (eth_is_on_demand_init())
{
eth_halt();
eth_set_current();
ret = eth_init();
if (ret < 0) {
eth_halt();
unregister_cli_job_cb(http_poll);
printf("HTTPD: no network interface found, failed to init network\n");
return;
}
}
else
{
eth_init_state_only();
}
}
if(!uip_initalized)
{
httpd_start();
handle_data_stream = http_update_image;
get_stream_upgrade_status = http_get_upgrade_status;
ip_tap = http_rcv;
uip_initalized = 1;
}
eth_rx();
httpd_poll_post_process();
}
#ifndef __BCMBCA_NET_H__
#define __BCMBCA_NET_H__
void http_poll(void);
int httpd_check_net_env(void);
#endif /* __BCMBCA_NET_H__ */
/**
* \addtogroup httpd
* @{
*/
/**
* \file
* HTTP server read-only file system header file.
* \author Adam Dunkels <adam@dunkels.com>
*/
/*
* Copyright (c) 2001, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: fs.h,v 1.6.2.3 2003/10/07 13:22:27 adam Exp $
*/
#ifndef __FS_H__
#define __FS_H__
#include "uip.h"
/**
* An open file in the read-only file system.
*/
struct fs_file {
char *data; /**< The actual file data. */
int len; /**< The length of the file data. */
};
/**
* Open a file in the read-only file system.
*
* \param name The name of the file.
*
* \param file The file pointer, which must be allocated by caller and
* will be filled in by the function.
*/
int fs_open(const char *name, struct fs_file *file);
#ifdef FS_STATISTICS
#if FS_STATISTICS == 1
u16_t fs_count(char *name);
#endif /* FS_STATISTICS */
#endif /* FS_STATISTICS */
/**
* Initialize the read-only file system.
*/
void fs_init(void);
#endif /* __FS_H__ */
unsigned char __404_html[] = {
0x3c, 0x21, 0x44, 0x4f, 0x43, 0x54, 0x59, 0x50, 0x45, 0x20, 0x48, 0x54,
0x4d, 0x4c, 0x3e, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0a, 0x09,
0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x6d, 0x65,
0x74, 0x61, 0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x22,
0x75, 0x74, 0x66, 0x2d, 0x38, 0x22, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x74,
0x69, 0x74, 0x6c, 0x65, 0x3e, 0x50, 0x61, 0x67, 0x65, 0x20, 0x6e, 0x6f,
0x74, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x3c, 0x2f, 0x74, 0x69, 0x74,
0x6c, 0x65, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x6c, 0x69, 0x6e, 0x6b, 0x20,
0x72, 0x65, 0x6c, 0x3d, 0x22, 0x69, 0x63, 0x6f, 0x6e, 0x22, 0x20, 0x68,
0x72, 0x65, 0x66, 0x3d, 0x22, 0x64, 0x61, 0x74, 0x61, 0x3a, 0x2c, 0x22,
0x3e, 0x0a, 0x09, 0x3c, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a, 0x09,
0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x64, 0x69,
0x76, 0x20, 0x69, 0x64, 0x3d, 0x22, 0x6d, 0x22, 0x3e, 0x0a, 0x09, 0x09,
0x09, 0x3c, 0x68, 0x31, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22,
0x72, 0x65, 0x64, 0x22, 0x3e, 0x50, 0x61, 0x67, 0x65, 0x20, 0x6e, 0x6f,
0x74, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x3c, 0x2f, 0x68, 0x31, 0x3e,
0x0a, 0x09, 0x09, 0x09, 0x3c, 0x70, 0x3e, 0x54, 0x68, 0x65, 0x20, 0x70,
0x61, 0x67, 0x65, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x77, 0x65, 0x72, 0x65,
0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x69, 0x6e, 0x67, 0x20, 0x66, 0x6f, 0x72,
0x20, 0x64, 0x6f, 0x65, 0x73, 0x6e, 0x27, 0x74, 0x20, 0x65, 0x78, 0x69,
0x73, 0x74, 0x21, 0x3c, 0x2f, 0x70, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x2f,
0x64, 0x69, 0x76, 0x3e, 0x0a, 0x09, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79,
0x3e, 0x0a, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0a
};
unsigned int __404_html_len = 274;
unsigned char fail_html[] = {
0x3c, 0x21, 0x44, 0x4f, 0x43, 0x54, 0x59, 0x50, 0x45, 0x20, 0x48, 0x54,
0x4d, 0x4c, 0x3e, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0a, 0x09,
0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x6d, 0x65,
0x74, 0x61, 0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x22,
0x75, 0x74, 0x66, 0x2d, 0x38, 0x22, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x74,
0x69, 0x74, 0x6c, 0x65, 0x3e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x20,
0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x3c, 0x2f, 0x74, 0x69, 0x74, 0x6c,
0x65, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x6c, 0x69, 0x6e, 0x6b, 0x20, 0x72,
0x65, 0x6c, 0x3d, 0x22, 0x69, 0x63, 0x6f, 0x6e, 0x22, 0x20, 0x68, 0x72,
0x65, 0x66, 0x3d, 0x22, 0x64, 0x61, 0x74, 0x61, 0x3a, 0x2c, 0x22, 0x3e,
0x0a, 0x09, 0x3c, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a, 0x09, 0x3c,
0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x64, 0x69, 0x76,
0x20, 0x69, 0x64, 0x3d, 0x22, 0x6d, 0x22, 0x3e, 0x0a, 0x09, 0x09, 0x09,
0x3c, 0x68, 0x31, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x72,
0x65, 0x64, 0x22, 0x3e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x20, 0x66,
0x61, 0x69, 0x6c, 0x65, 0x64, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0x0a, 0x09,
0x09, 0x09, 0x3c, 0x70, 0x3e, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20,
0x63, 0x6c, 0x69, 0x63, 0x6b, 0x20, 0x6f, 0x6e, 0x20, 0x72, 0x65, 0x66,
0x72, 0x65, 0x73, 0x68, 0x20, 0x62, 0x75, 0x74, 0x74, 0x6f, 0x6e, 0x20,
0x6f, 0x66, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x62, 0x72, 0x6f, 0x77,
0x73, 0x65, 0x72, 0x20, 0x74, 0x6f, 0x20, 0x67, 0x6f, 0x20, 0x62, 0x61,
0x63, 0x6b, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x75, 0x70,
0x6c, 0x6f, 0x61, 0x64, 0x20, 0x70, 0x61, 0x67, 0x65, 0x2e, 0x3c, 0x62,
0x72, 0x3e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x20, 0x55, 0x2d, 0x42, 0x6f,
0x6f, 0x74, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x6f, 0x6c, 0x65, 0x20, 0x66,
0x6f, 0x72, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72,
0x73, 0x2e, 0x3c, 0x2f, 0x70, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x2f, 0x64,
0x69, 0x76, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x73, 0x63, 0x72, 0x69, 0x70,
0x74, 0x3e, 0x0a, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x28, 0x20, 0x77,
0x69, 0x6e, 0x64, 0x6f, 0x77, 0x2e, 0x68, 0x69, 0x73, 0x74, 0x6f, 0x72,
0x79, 0x2e, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x53, 0x74, 0x61,
0x74, 0x65, 0x20, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x77,
0x69, 0x6e, 0x64, 0x6f, 0x77, 0x2e, 0x68, 0x69, 0x73, 0x74, 0x6f, 0x72,
0x79, 0x2e, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x53, 0x74, 0x61,
0x74, 0x65, 0x28, 0x20, 0x6e, 0x75, 0x6c, 0x6c, 0x2c, 0x20, 0x6e, 0x75,
0x6c, 0x6c, 0x2c, 0x20, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x2e, 0x6c,
0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x68, 0x72, 0x65, 0x66,
0x20, 0x29, 0x3b, 0x0a, 0x09, 0x09, 0x09, 0x7d, 0x0a, 0x09, 0x09, 0x09,
0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x63, 0x68, 0x65,
0x63, 0x6b, 0x45, 0x76, 0x74, 0x28, 0x29, 0x7b, 0x0a, 0x09, 0x09, 0x09,
0x09, 0x76, 0x61, 0x72, 0x20, 0x65, 0x76, 0x54, 0x79, 0x70, 0x65, 0x70,
0x3d, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x2e, 0x70, 0x65, 0x72, 0x66,
0x6f, 0x72, 0x6d, 0x61, 0x6e, 0x63, 0x65, 0x2e, 0x67, 0x65, 0x74, 0x45,
0x6e, 0x74, 0x72, 0x69, 0x65, 0x73, 0x42, 0x79, 0x54, 0x79, 0x70, 0x65,
0x28, 0x22, 0x6e, 0x61, 0x76, 0x69, 0x67, 0x61, 0x74, 0x69, 0x6f, 0x6e,
0x22, 0x29, 0x5b, 0x30, 0x5d, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x3b, 0x0a,
0x09, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x28, 0x65, 0x76, 0x54, 0x79,
0x70, 0x65, 0x70, 0x3d, 0x3d, 0x27, 0x72, 0x65, 0x6c, 0x6f, 0x61, 0x64,
0x27, 0x29, 0x7b, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x77, 0x69, 0x6e,
0x64, 0x6f, 0x77, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e,
0x2e, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x28, 0x22, 0x69, 0x6e,
0x64, 0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x22, 0x29, 0x3b, 0x0a,
0x09, 0x09, 0x09, 0x09, 0x7d, 0x0a, 0x09, 0x09, 0x09, 0x7d, 0x0a, 0x09,
0x09, 0x09, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x45, 0x76, 0x74, 0x28, 0x29,
0x3b, 0x0a, 0x09, 0x09, 0x3c, 0x2f, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74,
0x3e, 0x0a, 0x09, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0a, 0x3c,
0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0a
};
unsigned int fail_html_len = 679;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment