webportal.c 9.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2021 Raptor Engineering, LLC <sales@raptorengineering.com>
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include <logging/log.h>
LOG_MODULE_REGISTER(webportal, LOG_LEVEL_DBG);

#include <zephyr.h>
#include <posix/pthread.h>
#include <data/json.h>

#include "civetweb.h"

#include "raptor_extensions.h"
#include "static_files.h"

#define WITH_ZEPHYR 1
#include "kestrel.h"
21
#include <generated/mem.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#define STANDARD_HEADER(title)										\
"<head>" 												\
"<link rel=\"stylesheet\" href=\"style.css\"><meta charset=\"UTF-8\">"					\
"<title>" title "</title>"										\
"</head>"

#define SEND_STANDARD_TITLE_HEADER(conn, title)								\
civetweb_send_file_data(conn, STANDARD_HEADER(title), sizeof(STANDARD_HEADER(title)));

#define SEND_STANDARD_HEADER(conn, title)								\
SEND_STANDARD_TITLE_HEADER(conn, title)									\
civetweb_send_file_data(conn, embedded_file_header_inc, sizeof(embedded_file_header_inc)-1);

#define SEND_STANDARD_FOOTER(conn)									\
civetweb_send_file_data(conn, embedded_file_footer_inc, sizeof(embedded_file_footer_inc)-1);

#define KESTREL_WEBSERVICE_DEFINE_FILE_HANDLER(name, type)						\
static int static_file_##name##_handler(struct mg_connection *conn, void *cbdata)			\
{													\
	civetweb_send_file(conn, type, embedded_file_##name, sizeof(embedded_file_##name)-1);		\
	return 200;											\
}

46 47 48 49 50 51 52
#define KESTREL_WEBSERVICE_DEFINE_PSEUDO_FILE_HANDLER(name, type)					\
static int static_file_##name##_handler(struct mg_connection *conn, void *cbdata)			\
{													\
	civetweb_send_file(conn, type, embedded_file_##name##_pointer, embedded_file_##name##_length);	\
	return 200;											\
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#define KESTREL_WEBSERVICE_DEFINE_PAGE_HANDLER(name, type, title)					\
static int static_file_##name##_handler(struct mg_connection *conn, void *cbdata)			\
{													\
	mg_printf(conn,											\
		  "HTTP/1.1 200 OK\r\n"									\
		  "Content-Type: text/html\r\n"								\
		  "Connection: close\r\n\r\n");								\
	SEND_STANDARD_HEADER(conn, title)								\
	civetweb_send_file_data(conn, embedded_file_##name, sizeof(embedded_file_##name)-1);		\
	SEND_STANDARD_FOOTER(conn)									\
	return 200;											\
}

#define KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(name, path)						\
	mg_set_request_handler(ctx, path, static_file_##name##_handler, (void *)0);

69
#define MAX_REQUEST_SIZE_BYTES		8192
70 71 72 73 74

// Define ports
#define HTTP_PORT			8080
#define HTTPS_PORT			4443

75 76 77 78 79 80 81
uint8_t* embedded_file_fpga_firmware_raw_pointer = (uint8_t*)((uintptr_t)BMCSPIFLASH_BASE + 0x0);
size_t embedded_file_fpga_firmware_raw_length = 0x800000;
uint8_t* embedded_file_bmc_firmware_raw_pointer = (uint8_t*)((uintptr_t)BMCSPIFLASH_BASE + 0x800000);
size_t embedded_file_bmc_firmware_raw_length = 0x400000;
uint8_t* embedded_file_pnor_firmware_raw_pointer = (uint8_t*)((uintptr_t)HOSTSPIFLASH_BASE);
size_t embedded_file_pnor_firmware_raw_length = 0x400000;

82 83 84 85
KESTREL_WEBSERVICE_DEFINE_PAGE_HANDLER(index_html, "text/html", "Kestrel SoftBMC")
KESTREL_WEBSERVICE_DEFINE_FILE_HANDLER(style_css, "text/css")
KESTREL_WEBSERVICE_DEFINE_FILE_HANDLER(raptor_logo_jpg, "image/jpeg")
KESTREL_WEBSERVICE_DEFINE_FILE_HANDLER(kestrel_avatar_small_png, "image/png")
86 87 88
KESTREL_WEBSERVICE_DEFINE_PSEUDO_FILE_HANDLER(fpga_firmware_raw, "application/octet-stream")
KESTREL_WEBSERVICE_DEFINE_PSEUDO_FILE_HANDLER(bmc_firmware_raw, "application/octet-stream")
KESTREL_WEBSERVICE_DEFINE_PSEUDO_FILE_HANDLER(pnor_firmware_raw, "application/octet-stream")
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

struct civetweb_info {
	const char *version;
	const char *os;
	const char *compiler;
};

#define FIELD(struct_, member_, type_) { \
	.field_name = #member_, \
	.field_name_len = sizeof(#member_) - 1, \
	.offset = offsetof(struct_, member_), \
	.type = type_ \
}

void send_ok(struct mg_connection *conn)
{
	mg_printf(conn,
		  "HTTP/1.1 200 OK\r\n"
		  "Content-Type: text/html\r\n"
		  "Connection: close\r\n\r\n");
}

int system_info_handler(struct mg_connection *conn, void *cbdata)
{
	static const struct json_obj_descr descr[] = {
		FIELD(struct civetweb_info, version, JSON_TOK_STRING),
		FIELD(struct civetweb_info, os, JSON_TOK_STRING),
		FIELD(struct civetweb_info, compiler, JSON_TOK_STRING),
	};

	struct civetweb_info info = {};
	char info_str[1024] = {};
	int ret;
	int size;

	size = mg_get_system_info(info_str, sizeof(info_str));

	ret = json_obj_parse(info_str, size, descr, ARRAY_SIZE(descr), &info);

	send_ok(conn);

	if (ret < 0) {
		mg_printf(conn, "Could not retrieve: %d\n", ret);
		return 500;
	}

	SEND_STANDARD_HEADER(conn, "Kestrel Information")

	mg_printf(conn, "<h3>BMC information</h3>");
	mg_printf(conn, "<ul>\n");
	mg_printf(conn, "<li>BMC OS - %s</li>\n", info.os);
	mg_printf(conn, "<li>Server - civetweb %s</li>\n", info.version);
	mg_printf(conn, "<li>Compiler - %s</li>\n", info.compiler);
	mg_printf(conn, "<li>Board - %s</li>\n", CONFIG_BOARD);
	mg_printf(conn, "<li>Architecture - %s</li>\n", CONFIG_ARCH);
	mg_printf(conn, "</ul>\n");

	SEND_STANDARD_FOOTER(conn);

	return 200;
}

int firmware_upload_form_handler(struct mg_connection *conn, void *cbdata)
{
	mg_printf(conn,
	          "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
	          "close\r\n\r\n");

157
	SEND_STANDARD_HEADER(conn, "Firmware Management")
158 159 160 161 162 163 164 165 166 167

	mg_printf(conn, "<h3>Firmware Upload</h3>");
	mg_printf(conn,
	          "<form action=\"%s\" method=\"POST\" "
	          "enctype=\"multipart/form-data\">\n",
	          (const char *)cbdata);
	mg_printf(conn, "<input type=\"file\" name=\"firmwarefile\" multiple>\n");
	mg_printf(conn, "<input type=\"submit\" value=\"Upload\">\n");
	mg_printf(conn, "</form>\n");

168 169 170 171 172 173
	mg_printf(conn, "<p>\n");
	mg_printf(conn, "<h3>Firmware Download</h3>\n");
	mg_printf(conn, "<a href=\"/firmware/fpga.img\">FPGA</a><br>\n");
	mg_printf(conn, "<a href=\"/firmware/bmc.img\">BMC</a><br>\n");
	mg_printf(conn, "<a href=\"/firmware/pnor.img\">PNOR</a><br>\n");

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	SEND_STANDARD_FOOTER(conn);

	return 200;
}

int fw_field_received(const char *key, const char *filename, char *path, size_t pathlen, void *user_data)
{
	(void)key;
	(void)path;
	(void)pathlen;

	if (strcmp(key, "firmwarefile") == 0) {
		return MG_FORM_FIELD_STORAGE_GET;
	}

	return MG_FORM_FIELD_STORAGE_ABORT;
}


int fw_data_received(const char *key, const char *value, size_t valuelen, void *user_data)
{
	struct firmware_buffer_region *fw_data = (struct firmware_buffer_region *)user_data;

	if ((fw_data->current_write_offset + valuelen) > fw_data->buffer_length) {
		// Buffer overflow!
		// Discard the extra data...
		valuelen = fw_data->buffer_length - fw_data->current_write_offset;
		fw_data->overflow = 1;
	}
	memcpy(fw_data->buffer_address + fw_data->current_write_offset, value, valuelen);
	fw_data->current_write_offset += valuelen;
	fw_data->valid_bytes = fw_data->current_write_offset;

	return 0;
}

int firmware_upload_handler(struct mg_connection *conn, void *cbdata)
{
	const struct mg_request_info *req_info = mg_get_request_info(conn);
	int ret;
	struct mg_form_data_handler fdh = {fw_field_received,
	                                   fw_data_received,
	                                   0,
	                                   (void *)&main_firmware_buffer};

	(void)req_info;

	main_firmware_buffer.locked = 1;
	main_firmware_buffer.overflow = 0;
	main_firmware_buffer.current_write_offset = 0;
	main_firmware_buffer.valid_bytes = 0;

	// Call the form handler
	ret = mg_handle_form_request(conn, &fdh);

	// Clear out the remainder of the buffer
	memset(main_firmware_buffer.buffer_address + main_firmware_buffer.valid_bytes, 0xff, main_firmware_buffer.buffer_length - main_firmware_buffer.valid_bytes);

	main_firmware_buffer.locked = 0;

	mg_printf(conn,
	          "HTTP/1.1 200 OK\r\n"
	          "Content-Type: text/html\r\n"
	          "Connection: close\r\n\r\n");

	SEND_STANDARD_HEADER(conn, "Firmware Uploaded")

	mg_printf(conn, "<h3>Firmware Uploaded</h3>");
	mg_printf(conn, "<p>");
	mg_printf(conn, "File(s) uploaded:<br>");
	mg_printf(conn, "%i files<br>", ret);
	mg_printf(conn, "%lld bytes<br>", main_firmware_buffer.valid_bytes);

	if (main_firmware_buffer.overflow) {
		mg_printf(conn, "<p>WARNING: Data was discarded due to buffer overflow!");
		SEND_STANDARD_FOOTER(conn);
		return 500;
	}

	SEND_STANDARD_FOOTER(conn);
	return 200;
}

void *web_portal_pthread(void *arg)
{
	static const char * const options[] = {
		"listening_ports",
		STRINGIFY(HTTP_PORT),
		"num_threads",
263
		"16",
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		"max_request_size",
		STRINGIFY(MAX_REQUEST_SIZE_BYTES),
		NULL
	};

	struct mg_callbacks callbacks;
	struct mg_context *ctx;

	(void)arg;

	memset(&callbacks, 0, sizeof(callbacks));
	ctx = mg_start(&callbacks, 0, (const char **)options);

	if (ctx == NULL) {
		LOG_INF("Unable to start the server.");
		return 0;
	}

	// Static pages
	KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(index_html, "/$")
	KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(style_css, "/style.css$")

	// Static files
	KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(raptor_logo_jpg, "/raptor_logo.jpg$")
	KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(kestrel_avatar_small_png, "/kestrel_avatar_small.png$")
289 290 291
	KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(fpga_firmware_raw, "/firmware/fpga.img$")
	KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(bmc_firmware_raw, "/firmware/bmc.img$")
	KESTREL_WEBSERVICE_REGISTER_FILE_HANDLER(pnor_firmware_raw, "/firmware/pnor.img$")
292 293 294 295 296 297 298 299 300

	// Dynamic pages
	mg_set_request_handler(ctx, "/info$", system_info_handler, (void *)0);

	mg_set_request_handler(ctx, "/firmwareupload$", firmware_upload_form_handler, (void *)"/firmwareupload.callback");
	mg_set_request_handler(ctx, "/firmwareupload.callback$", firmware_upload_handler, (void *)0);

	return 0;
}