Patchwork [3/4] Libflashrom - Querying functions

login
register
about
Submitter Łukasz Dmitrowski
Date 2015-08-14 08:51:16
Message ID <55CDAC04.1060402@gmail.com>
Download mbox | patch
Permalink /patch/4316/
State New
Headers show

Comments

Łukasz Dmitrowski - 2015-08-14 08:51:16
Added querying functions

Signed-off-by: Lukasz Dmitrowski <lukasz.dmitrowski@gmail.com>


---
  Makefile      |   4 +-
  libflashrom.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
  libflashrom.h |  40 +++++++++++++++++
  3 files changed, 180 insertions(+), 3 deletions(-)

Patch

diff --git a/Makefile b/Makefile
index 2c7fccd..0c97268 100644
--- a/Makefile
+++ b/Makefile
@@ -726,7 +726,7 @@  FEATURE_CFLAGS += $(shell LC_ALL=C grep -q "UTSNAME := yes" .features && printf
  # We could use PULLED_IN_LIBS, but that would be ugly.
  FEATURE_LIBS += $(shell LC_ALL=C grep -q "NEEDLIBZ := yes" .libdeps && printf "%s" "-lz")
  
-LIBFLASHROM_OBJS = $(CHIP_OBJS) $(PROGRAMMER_OBJS) $(LIB_OBJS) udelay.o
+LIBFLASHROM_OBJS = $(CHIP_OBJS) $(PROGRAMMER_OBJS) $(LIB_OBJS)
  OBJS = $(CLI_OBJS) $(LIBFLASHROM_OBJS)
  
  all: hwlibs features $(PROGRAM)$(EXEC_SUFFIX) $(PROGRAM).8
@@ -737,7 +737,7 @@  endif
  $(PROGRAM)$(EXEC_SUFFIX): $(OBJS)
  	$(CC) $(LDFLAGS) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS) $(USBLIBS)
  
-libflashrom.a: $(LIBFLASHROM_OBJS)
+libflashrom.a: $(LIBFLASHROM_OBJS) print.o cli_common.o
  	$(AR) rcs $@ $^
  	$(RANLIB) $@
  
diff --git a/libflashrom.c b/libflashrom.c
index c96a2e9..3b94215 100644
--- a/libflashrom.c
+++ b/libflashrom.c
@@ -103,7 +103,144 @@  int print(const enum msglevel level, const char *const fmt, ...)
   * @{
   */
  
-/* TBD */
+/**
+ * @brief Returns flashrom version
+ * @return Flashrom version
+ */
+const char *fl_version(void)
+{
+        return flashrom_version;
+}
+
+/**
+ * @brief Returns a list of supported programmers
+ * @return List of supported programmers
+ */
+const char **fl_supported_programmers(void)
+{
+        enum programmer p = 0;
+        const char **supported_programmers = NULL;
+
+        supported_programmers = malloc((PROGRAMMER_INVALID + 1) * sizeof(char*));
+        if (!supported_programmers) {
+                msg_gerr("Out of memory!");
+        } else {
+                for (; p < PROGRAMMER_INVALID; ++p) {
+                        supported_programmers[p] = programmer_table[p].name;
+                }
+                supported_programmers[PROGRAMMER_INVALID] = NULL;
+        }
+
+        return supported_programmers;
+}
+
+/**
+ * @brief Returns list of supported flash ROM chips
+ * @return List of supported flash ROM chips
+ */
+fl_flashchip_info *fl_supported_flash_chips(void)
+{
+        int i = 0;
+        fl_flashchip_info *flashchip_info = NULL;
+
+        flashchip_info = malloc(flashchips_size * sizeof(fl_flashchip_info));
+        if (!flashchip_info) {
+                msg_gerr("Out of memory!");
+        } else {
+                for (; i < flashchips_size; ++i) {
+                        flashchip_info[i].vendor = flashchips[i].vendor;
+                        flashchip_info[i].name = flashchips[i].name;
+                        flashchip_info[i].tested.erase = flashchips[i].tested.erase;
+                        flashchip_info[i].tested.probe = flashchips[i].tested.probe;
+                        flashchip_info[i].tested.read = flashchips[i].tested.read;
+                        flashchip_info[i].tested.write = flashchips[i].tested.write;
+                        flashchip_info[i].total_size = flashchips[i].total_size;
+                }
+        }
+
+        return flashchip_info;
+}
+
+/**
+ * @brief Returns list of supported mainboards
+ * @return List of supported mainborads
+ */
+fl_board_info *fl_supported_boards(void)
+{
+        const struct board_info *binfo = boards_known;
+        fl_board_info *supported_boards = NULL;
+        int boards_known_size = 0;
+        int i = 0;
+
+        while ((binfo++)->vendor)
+                ++boards_known_size;
+        binfo = boards_known;
+        /* add place for {0} */
+        ++boards_known_size;
+
+        supported_boards = malloc(boards_known_size * sizeof(fl_board_info));
+
+        if (!supported_boards) {
+                msg_gerr("Out of memory!");
+        } else {
+                for (; i < boards_known_size; ++i) {
+                        supported_boards[i].vendor = binfo[i].vendor;
+                        supported_boards[i].name = binfo[i].name;
+                        supported_boards[i].working = binfo[i].working;
+                }
+        }
+
+        return supported_boards;
+}
+
+/**
+ * @brief Returns list of supported chipsets
+ * @return List of supported chipsets
+ */
+fl_chipset_info *fl_supported_chipsets(void)
+{
+        const struct penable *chipset = chipset_enables;
+        fl_chipset_info *supported_chipsets = NULL;
+        int chipset_enbales_size = 0;
+        int i = 0;
+
+        while ((chipset++)->vendor_name)
+                ++chipset_enbales_size;
+        chipset = chipset_enables;
+        /* add place for {0}*/
+        ++chipset_enbales_size;
+
+        supported_chipsets = malloc(chipset_enbales_size * sizeof(fl_chipset_info));
+
+        if (!supported_chipsets) {
+                msg_gerr("Out of memory!");
+        } else {
+                for (; i < chipset_enbales_size; ++i) {
+                        supported_chipsets[i].vendor = chipset[i].vendor_name;
+                        supported_chipsets[i].chipset = chipset[i].device_name;
+                        supported_chipsets[i].status = chipset[i].status;
+                }
+        }
+
+        return supported_chipsets;
+}
+
+/**
+ * @brief Frees fl_*_info and string lists allocated by flashrom
+ * @param Pointer to block of memory which should be freed
+ * @return 0 on success
+ *         1 on null pointer error
+ */
+int fl_data_free(void *const p)
+{
+        if (!p) {
+                msg_gerr("Null pointer!");
+                return 1;
+        } else {
+                free(p);
+                return 0;
+        }
+}
  
  /** @} */ /* end fl-query */
  
diff --git a/libflashrom.h b/libflashrom.h
index 2077407..5308fa3 100644
--- a/libflashrom.h
+++ b/libflashrom.h
@@ -38,6 +38,46 @@  typedef enum { /* This has to match enum msglevel. */
  typedef int(fl_log_callback_t)(fl_log_level_t, const char *format, va_list);
  void fl_set_log_callback(fl_log_callback_t *);
  
+typedef enum {
+        FL_TESTED_OK  = 0,
+        FL_TESTED_NT  = 1,
+        FL_TESTED_BAD = 2,
+        FL_TESTED_DEP = 3,
+        FL_TESTED_NA  = 4,
+} fl_test_state;
+
+typedef struct {
+        const char *vendor;
+        const char *name;
+        unsigned int total_size;
+
+        struct fl_tested {
+                fl_test_state probe;
+                fl_test_state read;
+                fl_test_state erase;
+                fl_test_state write;
+        } tested;
+} fl_flashchip_info;
+
+typedef struct {
+        const char *vendor;
+        const char *name;
+        fl_test_state working;
+} fl_board_info;
+
+typedef struct {
+        const char *vendor;
+        const char *chipset;
+        fl_test_state status;
+} fl_chipset_info;
+
+const char** fl_supported_programmers(void);
+const char* fl_version(void);
+fl_flashchip_info *fl_supported_flash_chips(void);
+fl_board_info *fl_supported_boards(void);
+fl_chipset_info *fl_supported_chipsets(void);
+int fl_data_free(void *const p);
+
  int fl_programmer_init(const char *prog_name, const char *prog_params);
  int fl_programmer_shutdown(void);