webportal.c 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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>

14
#include "simple_pwm.h"
15
#include "sbe_fsi.h"
16

17 18 19
#include "webportal.h"
#include "redfish.h"

20 21 22 23 24
#include "raptor_extensions.h"
#include "static_files.h"

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

#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;											\
}

50 51 52 53 54 55 56
#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;											\
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#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);

73
#define MAX_REQUEST_SIZE_BYTES		8192
74 75 76 77 78

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

79 80 81 82 83
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);
84
size_t embedded_file_pnor_firmware_raw_length = 0x4000000;
85

86 87 88 89
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")
90 91 92
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")
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

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);
148
	mg_printf(conn, "<li>Version - %d.%d.%d</li>\n", KESTREL_VERSION_MAJOR, KESTREL_VERSION_MINOR, KESTREL_VERSION_PATCHLEVEL);
149 150 151 152 153 154 155
	mg_printf(conn, "</ul>\n");

	SEND_STANDARD_FOOTER(conn);

	return 200;
}

156 157 158 159 160 161 162 163
const char* host_power_status_to_string(int power_status)
{
	if (power_status == HOST_POWER_STATUS_OFFLINE) {
		return "Offline";
	}
	else if (power_status == HOST_POWER_STATUS_POWERING_ON) {
		return "Powering On";
	}
164 165 166
	else if (power_status == HOST_POWER_STATUS_IPLING) {
		return "Initial Program Load";
	}
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	else if (power_status == HOST_POWER_STATUS_RUNNING) {
		return "Online";
	}
	else if (power_status == HOST_POWER_STATUS_POWERING_OFF) {
		return "Powering Off";
	}

	return "Unknown";
}

int host_status_handler(struct mg_connection *conn, void *cbdata)
{
	int i;
	uint8_t pwm_values[4] = {0, 0, 0, 0};
	int tach_values[4] = {0, 0, 0, 0};

	// Get status
#ifdef SIMPLEPWM_BASE
	for (i = 0; i < 4; i++) {
186 187
		pwm_values[i] = get_pwm_value((uint8_t*)SIMPLEPWM_BASE, i);
		tach_values[i] = get_tach_value((uint8_t*)SIMPLEPWM_BASE, i);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	}
#endif

	send_ok(conn);

	SEND_STANDARD_HEADER(conn, "Host Status")

	mg_printf(conn, "<h3>Host status</h3>");
	mg_printf(conn, "<p>");
	mg_printf(conn, "<b>Synoptic</b><br>");
	mg_printf(conn, "<div style=\"margin-left: 20px\">");
	mg_printf(conn, "System - %s<br>", host_power_status_to_string(host_power_status));
	mg_printf(conn, "</div>");
	mg_printf(conn, "<p>");
	mg_printf(conn, "<b>Mechanical</b><br>");
	mg_printf(conn, "<div style=\"margin-left: 20px\">");
	for (i = 0; i < 4; i++) {
		mg_printf(conn, "PWM channel %d - %d%% (%d RPM)<br>\n", i, (pwm_values[i] * 100) / 256, tach_values[i]);
	}
	mg_printf(conn, "</div>");
208 209 210 211 212 213 214 215 216 217 218
	if (host_cpu_temperature_sensor_count != 0)
	{
		mg_printf(conn, "<p>");
		mg_printf(conn, "<b>Thermal</b><br>");
		mg_printf(conn, "<div style=\"margin-left: 20px\">");
		for (i = 0; i < host_cpu_temperature_sensor_count; i++)
		{
			mg_printf(conn, "Sensor 0x%08x: %d° C<br>\n", host_cpu_temperature_sensor_data[i].sensor_id, host_cpu_temperature_sensor_data[i].temperature_c);
		}
		mg_printf(conn, "</div>");
	}
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 263 264 265 266 267 268 269 270 271 272 273 274 275
	mg_printf(conn, "<p>");
	mg_printf(conn, "<b>Control</b><br>");
	mg_printf(conn, "<div style=\"margin-left: 20px\">");
	mg_printf(conn, "<a href=\"/powercontrol?command=poweron\">Power on</a><br>");
	mg_printf(conn, "<a href=\"/powercontrol?command=poweroff\">Power off</a>");
	mg_printf(conn, "</div>");

	SEND_STANDARD_FOOTER(conn);

	return 200;
}

int power_control_handler(struct mg_connection *conn, void *cbdata)
{
	char command_buffer[128];

	// Parse input
	const struct mg_request_info *req_info = mg_get_request_info(conn);
	mg_get_var(req_info->query_string, strlen(req_info->query_string), "command", command_buffer, sizeof(command_buffer));
	command_buffer[sizeof(command_buffer)-1] = 0;

	send_ok(conn);

	SEND_STANDARD_HEADER(conn, "Power control")

	mg_printf(conn, "<h3>Command processing</h3>");
	mg_printf(conn, "<p>");

	if (strcmp(command_buffer, "poweron") == 0) {
		if (host_power_status == HOST_POWER_STATUS_OFFLINE) {
			mg_printf(conn, "Powering on host");
			power_on_host();
		}
		else {
			mg_printf(conn, "Host already online");
		}
	}
	else if (strcmp(command_buffer, "poweroff") == 0) {
		if (host_power_status == HOST_POWER_STATUS_OFFLINE) {
			mg_printf(conn, "Host already offline");
		}
		else {
			mg_printf(conn, "Powering off host");
			power_off_chassis();
		}
	}
	else {
		mg_printf(conn, "Invalid command!");
	}
	mg_printf(conn, "<p>");
	mg_printf(conn, "<a href=\"/hoststatus\">Back to host status page</a>");

	SEND_STANDARD_FOOTER(conn);

	return 200;
}

276 277
int firmware_upload_form_handler(struct mg_connection *conn, void *cbdata)
{
278
	send_ok(conn);
279

280
	SEND_STANDARD_HEADER(conn, "Firmware Management")
281 282 283 284 285 286 287 288 289 290

	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");

291 292 293 294 295 296
	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");

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	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;

357
	send_ok(conn);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

	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",
383
		"16",
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		"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$")
409 410 411
	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$")
412 413 414

	// Dynamic pages
	mg_set_request_handler(ctx, "/info$", system_info_handler, (void *)0);
415 416
	mg_set_request_handler(ctx, "/hoststatus$", host_status_handler, (void *)0);
	mg_set_request_handler(ctx, "/powercontrol$", power_control_handler, (void *)0);
417

418 419
	mg_set_request_handler(ctx, "/firmware$", firmware_upload_form_handler, (void *)"/firmware.upload");
	mg_set_request_handler(ctx, "/firmware.upload$", firmware_upload_handler, (void *)0);
420

421
	// Redfish
422
	register_redfish_request_handlers(ctx);
423

424 425
	return 0;
}