[PATCH RFC 1/3] lib: Define autoboot options device_type helpers

Geoff Levand geoff at infradead.org
Wed Dec 17 07:20:23 AEDT 2014


Hi Samuel,

On Tue, 2014-12-16 at 10:43 +1100, Samuel Mendoza-Jonas wrote:
> Signed-off-by: Samuel Mendoza-Jonas <sam.mj at au1.ibm.com>
> ---
>  lib/Makefile.am   |  1 +
>  lib/types/types.c | 37 +++++++++++++++++++++++++++++++++++++
>  lib/types/types.h | 14 ++++++++++++++
>  3 files changed, 52 insertions(+)
>  create mode 100644 lib/types/types.c
> 
> diff --git a/lib/Makefile.am b/lib/Makefile.am
> index fbf2ee2..b39cc9b 100644
> --- a/lib/Makefile.am
> +++ b/lib/Makefile.am
> @@ -38,6 +38,7 @@ lib_libpbcore_la_SOURCES = \
>  	lib/pb-config/pb-config.h \
>  	lib/process/process.c \
>  	lib/process/process.h \
> +	lib/types/types.c \
>  	lib/types/types.h \
>  	lib/talloc/talloc.c \
>  	lib/talloc/talloc.h \
> diff --git a/lib/types/types.c b/lib/types/types.c
> new file mode 100644
> index 0000000..b76f766
> --- /dev/null
> +++ b/lib/types/types.c
> @@ -0,0 +1,37 @@
> +#include <string.h>
> +#include <types/types.h>
> +
> +const char *device_type_name(enum device_type type)
> +{
> +	switch (type) {
> +	case DEVICE_TYPE_DISK:
> +		return "disk";
> +	case DEVICE_TYPE_OPTICAL:
> +		return "optical";
> +	case DEVICE_TYPE_NETWORK:
> +		return "network";
> +	case DEVICE_TYPE_ANY:
> +		return "any";
> +	case DEVICE_TYPE_UNKNOWN:
> +	default:
> +		return "unknown";

Are we going to need localized strings for this?  Is 'any' OK
for say the Chinese user?  Should we use a variable or
preprocessor macro for these strings and the ones in
find_device_type?  Maybe put the definition in types.h?

> +}
> +
> +int find_device_type(const char *str)

I guess this should be returning a type of 'enum device_type'?

> +{
> +	static const char *device_strings[4] = {
> +		"network",
> +		"disk",
> +		"optical",
> +		"any"
> +	};
> +	int i;
> +
> +	for (i = 0; i < 4; i++) {
> +		if (!strncmp(str, device_strings[i], strlen(device_strings[i])))
> +			return i;

I don't think you can rely on the compiler to assign
the values to the enum device_type's for this to work.
I think you need to do something like:

  if (!strncmp(str, "network", strlen("network"))
    return DEVICE_TYPE_NETWORK;

> +	}
> +
> +	return i;
> +}
> diff --git a/lib/types/types.h b/lib/types/types.h
> index aef1509..add81fc 100644
> --- a/lib/types/types.h
> +++ b/lib/types/types.h
> @@ -13,6 +13,9 @@ enum device_type {
>  	DEVICE_TYPE_UNKNOWN,
>  };
>  
> +const char *device_type_name(enum device_type type);
> +int find_device_type(const char *str);
> +
>  struct device {
>  	char		*id;
>  	enum device_type type;
> @@ -118,6 +121,17 @@ struct boot_priority {
>  	enum device_type	type;
>  };
>  
> +struct autoboot_option {
> +	enum {
> +		BOOT_DEVICE_TYPE,
> +		BOOT_DEVICE_UUID
> +	} boot_type;
> +	enum device_type	type;
> +	char			*uuid;
> +
> +	struct list_item        list;
> +};
> +
>  struct config {
>  	bool			autoboot_enabled;
>  	unsigned int		autoboot_timeout_sec;




More information about the Petitboot mailing list