iPXE
params.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27/** @file
28 *
29 * Request parameters
30 *
31 */
32
33#include <stdlib.h>
34#include <string.h>
35#include <ipxe/params.h>
36
37/** List of all parameter lists */
39
40/**
41 * Free request parameter list
42 *
43 * @v refcnt Reference count
44 */
45static void free_parameters ( struct refcnt *refcnt ) {
46 struct parameters *params =
48 struct parameter *param;
49 struct parameter *tmp;
50
51 DBGC ( params, "PARAMS \"%s\" destroyed\n", params->name );
52
53 /* Free all parameters */
55 list_del ( &param->list );
56 free ( param );
57 }
58
59 /* Free parameter list */
60 free ( params );
61}
62
63/**
64 * Find request parameter list by name
65 *
66 * @v name Parameter list name (may be NULL)
67 * @ret params Parameter list, or NULL if not found
68 */
69struct parameters * find_parameters ( const char *name ) {
70 struct parameters *params;
71
72 list_for_each_entry ( params, &parameters, list ) {
73 if ( ( params->name == name ) ||
74 ( params->name && name &&
75 ( strcmp ( params->name, name ) == 0 ) ) ) {
76 return params;
77 }
78 }
79 return NULL;
80}
81
82/**
83 * Create request parameter list
84 *
85 * @v name Parameter list name (may be NULL)
86 * @v method Method name (may be NULL)
87 * @ret params Parameter list, or NULL on failure
88 */
89struct parameters * create_parameters ( const char *name,
90 const char *method ) {
91 struct parameters *params;
92 size_t name_len;
93 size_t method_len;
94 char *name_copy;
95 char *method_copy;
96
97 /* Destroy any existing parameter list of this name */
98 params = find_parameters ( name );
99 if ( params ) {
100 claim_parameters ( params );
101 params_put ( params );
102 }
103
104 /* Allocate parameter list */
105 name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
106 method_len = ( method ? ( strlen ( method ) + 1 /* NUL */ ) : 0 );
107 params = zalloc ( sizeof ( *params ) + name_len + method_len );
108 if ( ! params )
109 return NULL;
110 ref_init ( &params->refcnt, free_parameters );
111 name_copy = ( ( ( void * ) params ) + sizeof ( *params ) );
112 method_copy = ( ( ( void * ) name_copy ) + name_len );
113
114 /* Populate parameter list */
115 if ( name ) {
116 strcpy ( name_copy, name );
117 params->name = name_copy;
118 }
119 if ( method ) {
120 strcpy ( method_copy, method );
121 params->method = method_copy;
122 }
123 INIT_LIST_HEAD ( &params->entries );
124
125 /* Add to list of parameter lists */
126 list_add_tail ( &params->list, &parameters );
127
128 DBGC ( params, "PARAMS \"%s\" created\n", params->name );
129 if ( params->method ) {
130 DBGC ( params, "PARAMS \"%s\" method is \"%s\"\n",
131 params->name, params->method );
132 }
133 return params;
134}
135
136/**
137 * Add request parameter
138 *
139 * @v params Parameter list
140 * @v key Parameter key
141 * @v value Parameter value
142 * @v flags Parameter flags
143 * @ret param Parameter, or NULL on failure
144 */
145struct parameter * add_parameter ( struct parameters *params,
146 const char *key, const char *value,
147 unsigned int flags ) {
148 struct parameter *param;
149 size_t key_len;
150 size_t value_len;
151 char *key_copy;
152 char *value_copy;
153
154 /* Allocate parameter */
155 key_len = ( strlen ( key ) + 1 /* NUL */ );
156 value_len = ( strlen ( value ) + 1 /* NUL */ );
157 param = zalloc ( sizeof ( *param ) + key_len + value_len );
158 if ( ! param )
159 return NULL;
160 key_copy = ( ( void * ) ( param + 1 ) );
161 value_copy = ( key_copy + key_len );
162
163 /* Populate parameter */
164 strcpy ( key_copy, key );
165 param->key = key_copy;
166 strcpy ( value_copy, value );
167 param->value = value_copy;
168 param->flags = flags;
169
170 /* Add to list of parameters */
171 list_add_tail ( &param->list, &params->entries );
172
173 DBGC ( params, "PARAMS \"%s\" added \"%s\"=\"%s\"%s%s\n",
174 params->name, param->key, param->value,
175 ( ( param->flags & PARAMETER_FORM ) ? " (form)" : "" ),
176 ( ( param->flags & PARAMETER_HEADER ) ? " (header)" : "" ) );
177 return param;
178}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
union @162305117151260234136356364136041353210355154177 key
pseudo_bit_t value[0x00020]
Definition arbel.h:2
const char * name
Definition ath9k_hw.c:1986
uint8_t flags
Flags.
Definition ena.h:7
#define DBGC(...)
Definition compiler.h:530
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
uint8_t method
Definition ib_mad.h:3
struct hv_monitor_parameter param[4][32]
Parameters.
Definition hyperv.h:13
String functions.
Request parameters.
#define PARAMETER_HEADER
Request parameter is a header parameter.
Definition params.h:46
#define PARAMETER_FORM
Request parameter is a form parameter.
Definition params.h:43
unsigned long tmp
Definition linux_pci.h:65
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition list.h:459
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
#define list_del(list)
Delete an entry from a list.
Definition list.h:120
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition list.h:46
#define LIST_HEAD(list)
Declare a static list head.
Definition list.h:38
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
struct parameters * create_parameters(const char *name, const char *method)
Create request parameter list.
Definition params.c:89
static void free_parameters(struct refcnt *refcnt)
Free request parameter list.
Definition params.c:45
struct parameter * add_parameter(struct parameters *params, const char *key, const char *value, unsigned int flags)
Add request parameter.
Definition params.c:145
struct parameters * find_parameters(const char *name)
Find request parameter list by name.
Definition params.c:69
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
char * strcpy(char *dest, const char *src)
Copy string.
Definition string.c:378
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A request parameter.
Definition params.h:31
struct list_head list
List of request parameters.
Definition params.h:33
A request parameter list.
Definition params.h:17
struct list_head list
List of all parameter lists.
Definition params.h:21
const char * name
Name.
Definition params.h:23
struct list_head entries
Parameters.
Definition params.h:27
struct refcnt refcnt
Reference count.
Definition params.h:19
const char * method
Request method.
Definition params.h:25
A reference counter.
Definition refcnt.h:27