shell.c 9.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// © 2020 - 2021 Raptor Engineering, LLC
//
// Released under the terms of the GPL v3
// See the LICENSE file for full details

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <shell/shell.h>
#include <drivers/uart.h>

#define WITH_ZEPHYR 1

#include "kestrel.h"
15
#include "simple_pwm.h"
16
#include "sbe_fsi.h"
17
#include "flash_filesystem.h"
18

19 20
#include <generated/mem.h>

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#define KESTREL_CONSOLE_THREAD_PRIORITY	K_PRIO_PREEMPT(CONFIG_NUM_PREEMPT_PRIORITIES - 1)
static K_THREAD_STACK_DEFINE(kestrel_console_thread_stack, 16384);
static struct k_thread kestrel_console_thread_data;

#define VERIFY_KESTREL_START_COMPLETE(shell)		\
		if (!kestrel_basic_init_complete) {	\
			print_kestrel_init_wait(shell);	\
			return -1;			\
		}

#define CONSOLE_ATTACH_BANNER "\n***** CONSOLE ATTACHED *****\n"

static void kestrel_console_thread(const void *p1, const void *p2)
{
	const struct shell *shell = p1;
	void *active_log_backend = (void*)p2;
	uint32_t active_log_level = CONFIG_LOG_DEFAULT_LEVEL;

39 40
	// Get shell printf context
	const struct shell_fprintf *sh_fprintf = shell->fprintf_ctx;
41 42 43 44 45

	k_mutex_lock(&shell->ctx->wr_mtx, K_FOREVER);

	shell_stop(shell);

46
	sh_fprintf->fwrite((const struct shell *)sh_fprintf->user_ctx, CONSOLE_ATTACH_BANNER, strlen(CONSOLE_ATTACH_BANNER));
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

	attach_to_host_console(shell);

	// Wait for host console to exit
	while (host_console_service_task_requested) {
		k_thread_suspend(kestrel_console_thread_id);
	}
	kestrel_console_thread_id = NULL;

	k_mutex_unlock(&shell->ctx->wr_mtx);

	// FIXME
	// HACK
	// Work around crash when attempting to select history (up arrow) after re-enabling the shell
	// The root cause of Zephyr crashing under the following conditions needs to be investigated
	// and fixed:
	// 1.) shell_stop()
	// 2.) Arbitrary data input to device driver underneath stopped shell
	// 3.) shell_start()
	// 4.) Up arrow input to scroll through history buffer
	if (IS_ENABLED(CONFIG_SHELL_HISTORY)) {
		z_shell_history_init(shell->history);
	}

	// Enable shell and logging
	if (IS_ENABLED(CONFIG_SHELL_LOG_BACKEND) && active_log_backend) {
		z_shell_log_backend_enable(shell->log_backend, (void *)shell, active_log_level);
	}
	shell_start(shell);
}

static int kestrel_shell_cmd_exit(const struct shell *shell, size_t argc, char **argv)
{
	shell->ctx->selected_cmd = NULL;

	return 0;
}

static int kestrel_shell_cmd_version(const struct shell *shell, size_t argc, char **argv)
{
	shell_print(shell, "%s", "0.9.0");

	return 0;
}

static void print_kestrel_init_wait(const struct shell *shell)
{
	shell_print(shell, "Kestrel is still starting up, please wait...");
}

static void print_kestrel_utility_command_usage(const struct shell *shell, char* command_name) {
	shell_print(shell, "Usage: %s <poweron chassison chassisoff>", command_name);
}

101
static void print_kestrel_reflash_command_usage(const struct shell *shell, char* command_name) {
102
	shell_print(shell, "Usage: %s <fpga bmc pnor>", command_name);
103 104 105 106 107 108
}

static void print_kestrel_memdump_command_usage(const struct shell *shell, char* command_name) {
	shell_print(shell, "Usage: %s <address> [length]", command_name);
}

109 110 111 112
static void print_kestrel_pwmcontrol_command_usage(const struct shell *shell, char* command_name) {
	shell_print(shell, "Usage: %s <channel> <value>", command_name);
}

113
static void print_kestrel_occcontrol_command_usage(const struct shell *shell, char* command_name) {
114
	shell_print(shell, "Usage: %s [poll]", command_name);
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
static int kestrel_shell_cmd_utility(const struct shell *shell, size_t argc, char **argv)
{
	if (argc < 2) {
		print_kestrel_utility_command_usage(shell, argv[0]);
		return -1;
	}

	if (strcmp(argv[1], "poweron") == 0) {
		VERIFY_KESTREL_START_COMPLETE(shell)

		return power_on_host();
	}
	else if (strcmp(argv[1], "chassisoff") == 0) {
		VERIFY_KESTREL_START_COMPLETE(shell)

		power_off_chassis();
		return 0;
	}
	else {
		shell_print(shell, "%s: Invalid parameter", argv[0]);
		print_kestrel_utility_command_usage(shell, argv[0]);
		return -1;
	}
}

142 143 144 145 146 147 148 149 150 151 152 153
static int kestrel_shell_cmd_reflash(const struct shell *shell, size_t argc, char **argv)
{
	if (argc < 2) {
		print_kestrel_reflash_command_usage(shell, argv[0]);
		return -1;
	}

	if (strcmp(argv[1], "pnor") == 0) {
		VERIFY_KESTREL_START_COMPLETE(shell)

		return write_flash_buffer_to_flash();
	}
154 155 156 157 158 159
	else if (strcmp(argv[1], "bmc") == 0) {
		return write_firmware_to_flash("bmc", 1, main_firmware_buffer.buffer_address, main_firmware_buffer.valid_bytes, 1);
	}
	else if (strcmp(argv[1], "fpga") == 0) {
		return write_firmware_to_flash("bmc", 0, main_firmware_buffer.buffer_address, main_firmware_buffer.valid_bytes, 0);
	}
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	else {
		shell_print(shell, "%s: Invalid parameter", argv[0]);
		print_kestrel_reflash_command_usage(shell, argv[0]);
		return -1;
	}
}

static int kestrel_shell_cmd_memdump(const struct shell *shell, size_t argc, char **argv)
{
	uintptr_t address;
	size_t length;
	int pos;
	int remaining;

	if (argc < 2) {
		print_kestrel_memdump_command_usage(shell, argv[0]);
		return -1;
	}

	address = strtoul(argv[1], NULL, 16);
	if (argc > 2) {
		length = strtoul(argv[2], NULL, 16);
	}
	else {
		length = 1;
	}

	for (pos = 0; pos < length; pos += remaining) {
		remaining = MIN(length - pos, SHELL_HEXDUMP_BYTES_IN_LINE);
		shell_hexdump_line(shell, address, (uint8_t*)address, remaining);
		address += remaining;
	}

	shell_print(shell, "");

	return 0;
}

198 199 200 201 202 203 204 205 206 207
static int kestrel_shell_cmd_pwmcontrol(const struct shell *shell, size_t argc, char **argv)
{
	int channel;
	int value;

	if (argc < 3) {
		print_kestrel_pwmcontrol_command_usage(shell, argv[0]);
		return -1;
	}

208 209
	channel = strtoul(argv[1], NULL, 10);
	value = strtoul(argv[2], NULL, 10);
210 211 212 213 214 215 216 217 218 219 220 221

#ifdef SIMPLEPWM_BASE
	set_pwm_value((uint8_t*)SIMPLEPWM_BASE, channel, value);

	shell_print(shell, "");
#else
	shell_print(shell, "[ERROR] No PWM controller available!");
#endif

	return 0;
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235
static int kestrel_shell_cmd_occcontrol(const struct shell *shell, size_t argc, char **argv)
{
	int sensor_number;
	uint8_t response_data[IBM_POWER9_SBE_OCC_RESPONSE_SIZE];
	uint8_t occ_version_string[17];
	size_t response_length = 0;
	struct ibm_power9_occ_poll_response_header_t *occ_response_header;

	if (argc < 2) {
		print_kestrel_occcontrol_command_usage(shell, argv[0]);
		return -1;
	}

	if (strcmp(argv[1], "poll") == 0) {
236 237 238 239 240
		if (k_mutex_lock(&occ_access_mutex, K_MSEC(1000)) != 0) {
			printk("Unable to acquire OCC mutex in a timely manner! %s:%d\n", __FILE__, __LINE__);
			return -EAGAIN;
		}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		if (occ_poll(0, 0x2400, IBM_POWER9_SBE_OCC_RESPONSE_SIZE, response_data, &response_length))
		{
			shell_print(shell, "OCC not responding!");
		}
		else
		{
			occ_response_header = (struct ibm_power9_occ_poll_response_header_t*)response_data;
			memcpy(occ_version_string, occ_response_header->occ_version_string, 16);
			occ_version_string[16] = 0;
			shell_print(shell, "Found OCC version '%s'", occ_version_string);
			shell_print(shell, "Found %d temperature sensors", host_cpu_temperature_sensor_count);
			for (sensor_number = 0; sensor_number < host_cpu_temperature_sensor_count; sensor_number++)
			{
				shell_print(shell, "Temperature sensor 0x%08x: %d° C", host_cpu_temperature_sensor_data[sensor_number].sensor_id, host_cpu_temperature_sensor_data[sensor_number].temperature_c);
			}
		}
257 258 259 260 261 262 263

		k_mutex_unlock(&occ_access_mutex);
	}
	else {
		shell_print(shell, "%s: Invalid parameter", argv[0]);
		print_kestrel_occcontrol_command_usage(shell, argv[0]);
		return -1;
264 265 266 267 268
	}

	return 0;
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static int kestrel_shell_cmd_console(const struct shell *shell, size_t argc, char **argv)
{
	const void *active_log_backend = NULL;

	VERIFY_KESTREL_START_COMPLETE(shell)

	if (host_console_service_task_requested) {
		shell_print(shell, "%s: Console already active, aborting", argv[0]);
		return -1;
	}

	if (IS_ENABLED(CONFIG_SHELL_LOG_BACKEND)) {
		active_log_backend = shell->log_backend;
		z_shell_log_backend_disable(shell->log_backend);
	}

	kestrel_console_thread_id = k_thread_create(&kestrel_console_thread_data, kestrel_console_thread_stack,
			K_THREAD_STACK_SIZEOF(kestrel_console_thread_stack),
			(k_thread_entry_t)kestrel_console_thread,
			(void*)shell, (void*)active_log_backend, NULL, KESTREL_CONSOLE_THREAD_PRIORITY, 0, K_NO_WAIT);
	k_thread_name_set(kestrel_console_thread_id, "kestrel_console");

	return 0;
}

/* Subcommand array for "kestrel" (level 1). */
SHELL_STATIC_SUBCMD_SET_CREATE(sub_kestrel,
	// Exit to main shell
	SHELL_CMD(exit, NULL, "Exit to main shell", kestrel_shell_cmd_exit),
	// Application version
	SHELL_CMD(version, NULL, "Kestrel main application version", kestrel_shell_cmd_version),
	// OpenBMC utility command (compat)
	SHELL_CMD(obmcutil, NULL, "OBMC-compatible utilty command", kestrel_shell_cmd_utility),
	// OpenBMC console command (compat)
	SHELL_CMD(obmc-console-client, NULL, "OBMC-compatible console command", kestrel_shell_cmd_console),
304 305 306 307
	// Flash programming command
	SHELL_CMD(reflash, NULL, "Simple Flash utility", kestrel_shell_cmd_reflash),
	// Arbitrary memory dump command
	SHELL_CMD(memdump, NULL, "Memory dump tool", kestrel_shell_cmd_memdump),
308 309
	// PWM commands
	SHELL_CMD(pwmcontrol, NULL, "PWM controller", kestrel_shell_cmd_pwmcontrol),
310 311
	// OCC commands
	SHELL_CMD(occcontrol, NULL, "OCC command interface", kestrel_shell_cmd_occcontrol),
312 313 314 315 316
	// Command list array terminator
	SHELL_SUBCMD_SET_END
);

// Register main kestrel command prefix
317
SHELL_CMD_REGISTER(kestrel, &sub_kestrel, "Kestrel commands", NULL);