2018年7月6日 星期五

UEFI Application - Python 3rd modules

今天想要試試如何在 UEFI 使用 Python 3rd modules
故挑了一個常用的 requests module 來測試

底下是相關的步驟
我使用的是 UDK 2014

1. pip install requests
2. pip show requests
3. copy step2 的相關 modules to EFI\StdLib\lib\python.27\site-packages
4. uncomment "default_socket_options = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]" in site-packages\urllib3\connection.py, and add "default_socket_options = []"
5. open "signal", "unicodedata", "zipimport", "zlib" in Python\Efi\config.c
6. define "WITH_THREAD" in Python\X64\pyconfig.h
7. add "Python-$(PYTHON_VERSION)/Modules/threadmodule.c" and "Python-$(PYTHON_VERSION)/Python/thread.c" in Python\PythonCore.inf
8. add your thread implement for uefi or you can use mine below. if this file is named "xxxx.h", remember to add "#include "xxxx.h" in Python\Python-2.7.2\Python\thread.c
9. rebuild Python.efi



/*
 * Initialization.
 */
static void
PyThread__init_thread(void)
{
}

/*
 * Thread support.
 */
static int g_id = 123;
long
PyThread_start_new_thread(void (*func)(void *), void *arg)
{
    return g_id;
}

long
PyThread_get_thread_ident(void)
{
    return g_id;
}

void
PyThread_exit_thread(void)
{
}

/*
 * Lock support.
 */

typedef struct {
    char             locked; /* 0=unlocked, 1=locked */
} thread_lock;


PyThread_type_lock
PyThread_allocate_lock(void)
{
    thread_lock *lock = (thread_lock*)malloc(sizeof(thread_lock));
    lock->locked = 0;

    return (PyThread_type_lock)lock;
}

void
PyThread_free_lock(PyThread_type_lock lock)
{
    thread_lock *p = (thread_lock*)lock;

    free((void*)p);
}

int
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
{
    thread_lock *p = (thread_lock*)lock;

    int success = p->locked == 0;

    if ( !success && waitflag ) {
        int count = 0;
        while ( p->locked ) {
            count++;
        }
        success = 1;
    }

    if (success) p->locked = 1;
    return success;
}

void
PyThread_release_lock(PyThread_type_lock lock)
{
    thread_lock *p = (thread_lock*)lock;
    p->locked = 0;
}

沒有留言:

張貼留言