summaryrefslogtreecommitdiff
path: root/plugins/bootimg/python
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/bootimg/python')
-rw-r--r--plugins/bootimg/python/Makefile.am20
-rw-r--r--plugins/bootimg/python/format.c311
-rw-r--r--plugins/bootimg/python/format.h42
-rw-r--r--plugins/bootimg/python/module.c82
-rw-r--r--plugins/bootimg/python/module.h38
-rw-r--r--plugins/bootimg/python/translate.c110
-rw-r--r--plugins/bootimg/python/translate.h41
7 files changed, 644 insertions, 0 deletions
diff --git a/plugins/bootimg/python/Makefile.am b/plugins/bootimg/python/Makefile.am
new file mode 100644
index 0000000..a79af4d
--- /dev/null
+++ b/plugins/bootimg/python/Makefile.am
@@ -0,0 +1,20 @@
+
+noinst_LTLIBRARIES = libbootimgpython.la
+
+libbootimgpython_la_SOURCES = \
+ format.h format.c \
+ module.h module.c \
+ translate.h translate.c
+
+libbootimgpython_la_LDFLAGS =
+
+
+devdir = $(includedir)/chrysalide-$(subdir)
+
+dev_HEADERS = $(libbootimgpython_la_SOURCES:%c=)
+
+
+AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \
+ -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS)
diff --git a/plugins/bootimg/python/format.c b/plugins/bootimg/python/format.c
new file mode 100644
index 0000000..ac987fc
--- /dev/null
+++ b/plugins/bootimg/python/format.c
@@ -0,0 +1,311 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * format.c - équivalent Python du fichier "plugins/bootimg/format.c"
+ *
+ * Copyright (C) 2013-2017 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#include "format.h"
+
+
+#include <pygobject.h>
+
+
+#include <i18n.h>
+#include <plugins/pychrysalide/helpers.h>
+#include <plugins/pychrysalide/analysis/content.h>
+#include <plugins/pychrysalide/format/known.h>
+
+
+#include "translate.h"
+#include "../format.h"
+
+
+
+/* Crée un nouvel objet Python de type 'BootImgFormat'. */
+static PyObject *py_bootimg_format_new(PyTypeObject *, PyObject *, PyObject *);
+
+/* Fournit l'en-tête Bootimg correspondant au format. */
+static PyObject *py_bootimg_format_get_header(PyObject *, void *);
+
+/* Fournit le noyau inclus dans une image de démarrage. */
+static PyObject *py_bootimg_format_get_kernel(PyObject *, void *);
+
+/* Fournit le disque en RAM inclus dans une image de démarrage. */
+static PyObject *py_bootimg_format_get_ramdisk(PyObject *, void *);
+
+
+
+/******************************************************************************
+* *
+* Paramètres : type = type de l'objet à instancier. *
+* args = arguments fournis à l'appel. *
+* kwds = arguments de type key=val fournis. *
+* *
+* Description : Crée un nouvel objet Python de type 'BootImgFormat'. *
+* *
+* Retour : Instance Python mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bootimg_format_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Instance GLib du contenu */
+ int ret; /* Bilan de lecture des args. */
+ GBootImgFormat *format; /* Création GLib à transmettre */
+
+#define BOOTIMG_FORMAT_DOC \
+ "BootImgFormat provides support for Android boot images.\n" \
+ "\n" \
+ "Instances can be created using the following constructor:\n" \
+ "\n" \
+ " BootImgFormat(content)" \
+ "\n" \
+ "Where content is the binary content of a file usually named 'boot.img'."
+
+ ret = PyArg_ParseTuple(args, "O&", convert_to_binary_content, &content);
+ if (!ret) return NULL;
+
+ format = g_bootimg_format_new(content);
+
+ if (format == NULL)
+ {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+
+ else
+ {
+ result = pygobject_new(G_OBJECT(format));
+ g_object_unref(format);
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* closure = non utilisé ici. *
+* *
+* Description : Fournit l'entête Bootimg correspondant au format. *
+* *
+* Retour : Structure Python créée pour l'occasion. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bootimg_format_get_header(PyObject *self, void *closure)
+{
+ PyObject *result; /* Trouvaille à retourner */
+ GBootImgFormat *format; /* Version GLib du format */
+ const boot_img_hdr *header; /* Entête à transcrire */
+
+#define BOOTING_FORMAT_HEADER_ATTRIB PYTHON_GET_DEF_FULL \
+( \
+ header, py_bootimg_format, \
+ "Header of the boot image." \
+)
+
+ format = G_BOOTIMG_FORMAT(pygobject_get(self));
+
+ header = g_bootimg_format_get_header(format);
+
+ result = translate_bootimg_header_to_python(header);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = objet Python concerné par l'appel. *
+* closure = non utilisé ici. *
+* *
+* Description : Fournit le noyau inclus dans une image de démarrage. *
+* *
+* Retour : Nouveau contenu binaire ou NULL en cas d'absence. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bootimg_format_get_kernel(PyObject *self, void *closure)
+{
+ PyObject *result; /* Trouvailles à retourner */
+ GBootImgFormat *format; /* Version GLib du format */
+ GBinContent *content; /* Contenu à transmettre */
+
+#define BOOTING_FORMAT_KERNEL_ATTRIB PYTHON_GET_DEF_FULL \
+( \
+ kernel, py_bootimg_format, \
+ "Binary content for the (Linux) kernel contained in the boot image, or None." \
+)
+
+ format = G_BOOTIMG_FORMAT(pygobject_get(self));
+
+ content = g_bootimg_format_get_ramdisk(format);
+
+ if (content == NULL)
+ {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+
+ else
+ {
+ g_object_ref_sink(content);
+ result = pygobject_new(G_OBJECT(content));
+ g_object_unref(content);
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = objet Python concerné par l'appel. *
+* closure = non utilisé ici. *
+* *
+* Description : Fournit le disque en RAM inclus dans une image de démarrage. *
+* *
+* Retour : Nouveau contenu binaire ou None en cas d'absence. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bootimg_format_get_ramdisk(PyObject *self, void *closure)
+{
+ PyObject *result; /* Trouvailles à retourner */
+ GBootImgFormat *format; /* Version GLib du format */
+ GBinContent *content; /* Contenu à transmettre */
+
+#define BOOTING_FORMAT_RAMDISK_ATTRIB PYTHON_GET_DEF_FULL \
+( \
+ ramdisk, py_bootimg_format, \
+ "Binary content for the ramdisk contained in the boot image, or None." \
+)
+
+ format = G_BOOTIMG_FORMAT(pygobject_get(self));
+
+ content = g_bootimg_format_get_ramdisk(format);
+
+ if (content == NULL)
+ {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+
+ else
+ {
+ g_object_ref_sink(content);
+ result = pygobject_new(G_OBJECT(content));
+ g_object_unref(content);
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Fournit un accès à une définition de type à diffuser. *
+* *
+* Retour : Définition d'objet pour Python. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+PyTypeObject *get_python_bootimg_format_type(void)
+{
+ static PyMethodDef py_bootimg_format_methods[] = {
+ { NULL }
+ };
+
+ static PyGetSetDef py_bootimg_format_getseters[] = {
+ BOOTING_FORMAT_HEADER_ATTRIB,
+ BOOTING_FORMAT_KERNEL_ATTRIB,
+ BOOTING_FORMAT_RAMDISK_ATTRIB,
+ { NULL }
+ };
+
+ static PyTypeObject py_bootimg_format_type = {
+
+ PyVarObject_HEAD_INIT(NULL, 0)
+
+ .tp_name = "pychrysalide.format.bootimg.BootImgFormat",
+ .tp_basicsize = sizeof(PyGObject),
+
+ .tp_flags = Py_TPFLAGS_DEFAULT,
+
+ .tp_doc = BOOTIMG_FORMAT_DOC,
+
+ .tp_methods = py_bootimg_format_methods,
+ .tp_getset = py_bootimg_format_getseters,
+ .tp_new = py_bootimg_format_new
+
+ };
+
+ return &py_bootimg_format_type;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : module = module dont la définition est à compléter. *
+* *
+* Description : Prend en charge l'objet 'pychrysalide.format..BootImgFormat'.*
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool register_python_bootimg_format(PyObject *module)
+{
+ PyTypeObject *type; /* Type Python 'BootImgFormat' */
+ PyObject *dict; /* Dictionnaire du module */
+
+ type = get_python_bootimg_format_type();
+
+ dict = PyModule_GetDict(module);
+
+ if (!register_class_for_pygobject(dict, G_TYPE_BOOTIMG_FORMAT, type, get_python_known_format_type()))
+ return false;
+
+ return true;
+
+}
diff --git a/plugins/bootimg/python/format.h b/plugins/bootimg/python/format.h
new file mode 100644
index 0000000..495d46e
--- /dev/null
+++ b/plugins/bootimg/python/format.h
@@ -0,0 +1,42 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * format.h - prototypes pour l'équivalent Python du fichier "plugins/bootimg/format.h"
+ *
+ * Copyright (C) 2013-2017 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#ifndef _PLUGINS_BOOTIMG_PYTHON_FORMAT_H
+#define _PLUGINS_BOOTIMG_PYTHON_FORMAT_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+
+/* Fournit un accès à une définition de type à diffuser. */
+PyTypeObject *get_python_bootimg_format_type(void);
+
+/* Prend en charge l'objet 'pychrysalide.format.bootimg.BootImgFormat'. */
+bool register_python_bootimg_format(PyObject *);
+
+
+
+#endif /* _PLUGINS_BOOTIMG_PYTHON_FORMAT_H */
diff --git a/plugins/bootimg/python/module.c b/plugins/bootimg/python/module.c
new file mode 100644
index 0000000..ee36ecf
--- /dev/null
+++ b/plugins/bootimg/python/module.c
@@ -0,0 +1,82 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * module.c - intégration du répertoire bootimg en tant que module
+ *
+ * Copyright (C) 2019 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#include "module.h"
+
+
+#include <Python.h>
+
+
+#include <plugins/pychrysalide/access.h>
+#include <plugins/pychrysalide/helpers.h>
+
+
+#include "format.h"
+
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Ajoute le module 'format.bootimg' au module Python. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool add_format_bootimg_module_to_python_module(void)
+{
+ bool result; /* Bilan à retourner */
+ PyObject *super; /* Module à compléter */
+ PyObject *module; /* Sous-module mis en place */
+
+ static PyModuleDef py_chrysalide_bootimg_module = {
+
+ .m_base = PyModuleDef_HEAD_INIT,
+
+ .m_name = "pychrysalide.format.bootimg",
+ .m_doc = "Python module for Chrysalide.format.bootimg",
+
+ .m_size = -1,
+
+ };
+
+ result = false;
+
+ super = get_access_to_python_module("pychrysalide.format");
+
+ module = build_python_module(super, &py_chrysalide_bootimg_module);
+
+ result = (module != NULL);
+
+ if (result) result = register_python_bootimg_format(module);
+
+ assert(result);
+
+ return result;
+
+}
diff --git a/plugins/bootimg/python/module.h b/plugins/bootimg/python/module.h
new file mode 100644
index 0000000..d78ab38
--- /dev/null
+++ b/plugins/bootimg/python/module.h
@@ -0,0 +1,38 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * module.h - prototypes pour l'intégration du répertoire bootimg en tant que module
+ *
+ * Copyright (C) 2019 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#ifndef _PLUGINS_BOOTIMG_PYTHON_MODULE_H
+#define _PLUGINS_BOOTIMG_PYTHON_MODULE_H
+
+
+#include <stdbool.h>
+
+
+
+/* Ajoute le module 'format.bootimg' au module Python. */
+bool add_format_bootimg_module_to_python_module(void);
+
+
+
+#endif /* _PLUGINS_BOOTIMG_PYTHON_MODULE_H */
diff --git a/plugins/bootimg/python/translate.c b/plugins/bootimg/python/translate.c
new file mode 100644
index 0000000..6d982c7
--- /dev/null
+++ b/plugins/bootimg/python/translate.c
@@ -0,0 +1,110 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * translate.c - conversion de structures ELF en objets Python
+ *
+ * Copyright (C) 2017 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#include "translate.h"
+
+
+#include <assert.h>
+
+
+#include <plugins/pychrysalide/struct.h>
+
+
+
+/******************************************************************************
+* *
+* Paramètres : header = entête BOOT.img à décrire en Python. *
+* *
+* Description : Traduit un entête BOOT.img en Python. *
+* *
+* Retour : Structure mise en place ou NULL en cas d'erreur. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+PyObject *translate_bootimg_header_to_python(const boot_img_hdr *header)
+{
+ PyObject *result; /* Construction à retourner */
+ PyTypeObject *base; /* Modèle d'objet à créer */
+ PyObject *attrib; /* Attribut à constituer */
+ int ret; /* Bilan d'une mise en place */
+
+ base = get_python_py_struct_type();
+
+ result = PyObject_CallFunction((PyObject *)base, NULL);
+ assert(result != NULL);
+
+
+#define TRANSLATE_HEADER_BYTE_VALUE(_f, _sz) \
+ do \
+ { \
+ attrib = PyBytes_FromStringAndSize((char *)header->_f, _sz); \
+ ret = PyDict_SetItemString(result, #_f, attrib); \
+ if (ret != 0) goto failed; \
+ } \
+ while (0);
+
+#define TRANSLATE_HEADER_NUMERIC_VALUE(_f) \
+ do \
+ { \
+ attrib = PyLong_FromUnsignedLongLong(header->_f); \
+ ret = PyDict_SetItemString(result, #_f, attrib); \
+ if (ret != 0) goto failed; \
+ } \
+ while (0);
+
+
+ TRANSLATE_HEADER_BYTE_VALUE(magic, BOOT_MAGIC_SIZE);
+ TRANSLATE_HEADER_NUMERIC_VALUE(kernel_size);
+ TRANSLATE_HEADER_NUMERIC_VALUE(kernel_addr);
+
+ TRANSLATE_HEADER_NUMERIC_VALUE(ramdisk_size);
+ TRANSLATE_HEADER_NUMERIC_VALUE(ramdisk_addr);
+
+ TRANSLATE_HEADER_NUMERIC_VALUE(second_size);
+ TRANSLATE_HEADER_NUMERIC_VALUE(second_addr);
+
+ TRANSLATE_HEADER_NUMERIC_VALUE(tags_addr);
+ TRANSLATE_HEADER_NUMERIC_VALUE(page_size);
+ TRANSLATE_HEADER_NUMERIC_VALUE(header_version);
+ TRANSLATE_HEADER_NUMERIC_VALUE(os_version);
+ TRANSLATE_HEADER_BYTE_VALUE(name, BOOT_NAME_SIZE);
+ TRANSLATE_HEADER_BYTE_VALUE(cmdline, BOOT_ARGS_SIZE);
+ TRANSLATE_HEADER_BYTE_VALUE(id, 8 * sizeof(uint32_t));
+ TRANSLATE_HEADER_BYTE_VALUE(extra_cmdline, BOOT_EXTRA_ARGS_SIZE);
+
+ TRANSLATE_HEADER_NUMERIC_VALUE(recovery_dtbo_size);
+ TRANSLATE_HEADER_NUMERIC_VALUE(recovery_dtbo_offset);
+ TRANSLATE_HEADER_NUMERIC_VALUE(header_size);
+
+ return result;
+
+ failed:
+
+ Py_DECREF(result);
+
+ return NULL;
+
+}
diff --git a/plugins/bootimg/python/translate.h b/plugins/bootimg/python/translate.h
new file mode 100644
index 0000000..1402dc7
--- /dev/null
+++ b/plugins/bootimg/python/translate.h
@@ -0,0 +1,41 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * translate.h - prototypes pour la conversion de structures BOOT.img en objets Python
+ *
+ * Copyright (C) 2019 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * Chrysalide is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Chrysalide is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#ifndef _PLUGINS_BOOTIMG_PYTHON_TRANSLATE_H
+#define _PLUGINS_BOOTIMG_PYTHON_TRANSLATE_H
+
+
+#include <Python.h>
+
+
+#include "../bootimg-def.h"
+
+
+
+/* Traduit un entête BOOT.img en Python. */
+PyObject *translate_bootimg_header_to_python(const boot_img_hdr *);
+
+
+
+#endif /* _PLUGINS_BOOTIMG_PYTHON_TRANSLATE_H */