summaryrefslogtreecommitdiffstats
path: root/src/hci/commands/nvo_cmd.c
blob: 4c453c77fdb3da701d744f152e5bdc9efcd155a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
101
102
103
104
105
106
107
108
109
110
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <gpxe/nvo.h>
#include <gpxe/dhcp.h>
#include <gpxe/settings.h>
#include <gpxe/command.h>

extern struct nvo_block *ugly_nvo_hack;

static int show_exec ( int argc, char **argv ) {
	struct config_context dummy_context;
	char buf[256];
	int rc;

	if ( ! ugly_nvo_hack ) {
		printf ( "No non-volatile option storage available\n" );
		return 1;
	}

	if ( argc != 2 ) {
		printf ( "Syntax: %s <identifier>\n", argv[0] );
		return 1;
	}

	dummy_context.options = ugly_nvo_hack->options;
	if ( ( rc = show_named_setting ( &dummy_context, argv[1], buf,
					 sizeof ( buf ) ) ) < 0 ) {
		printf ( "Could not find \"%s\": %s\n",
			 argv[1], strerror ( -rc ) );
		return 1;
	}

	printf ( "%s = %s\n", argv[1], buf );
	return 0;
}

static int set_exec ( int argc, char **argv ) {
	struct config_context dummy_context;
	int rc;

	if ( ! ugly_nvo_hack ) {
		printf ( "No non-volatile option storage available\n" );
		return 1;
	}

	if ( argc != 3 ) {
		printf ( "Syntax: %s <identifier> <value>\n",
			 argv[0] );
		return 1;
	}

	dummy_context.options = ugly_nvo_hack->options;
	if ( ( rc = set_named_setting ( &dummy_context, argv[1],
					argv[2] ) ) != 0 ) {
		printf ( "Could not set \"%s\"=\"%s\": %s\n",
			 argv[1], argv[2], strerror ( -rc ) );
		return 1;
	}
	
	if ( nvo_save ( ugly_nvo_hack ) != 0 ) {
		printf ( "Could not save options to non-volatile storage\n" );
		return 1;
	}

	return 0;
}

static int clear_exec ( int argc, char **argv ) {
	struct config_context dummy_context;
	int rc;

	if ( ! ugly_nvo_hack ) {
		printf ( "No non-volatile option storage available\n" );
		return 1;
	}

	if ( argc != 2 ) {
		printf ( "Syntax: %s <identifier>\n",
			 argv[0] );
		return 1;
	}

	dummy_context.options = ugly_nvo_hack->options;
	if ( ( rc = clear_named_setting ( &dummy_context, argv[1] ) ) != 0 ) {
		printf ( "Could not clear \"%s\": %s\n",
			 argv[1], strerror ( -rc ) );
		return 1;
	}
	
	return 0;
}

struct command nvo_commands[] __command = {
	{
		.name = "show",
		.exec = show_exec,
	},
	{
		.name = "set",
		.exec = set_exec,
	},	
	{
		.name = "clear",
		.exec = clear_exec,
	},
};