From 8f35f2f5b5acf3083c211ae1fb708463b0debf72 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 25 Dec 2015 15:08:26 +0100 Subject: [PATCH] More protocol details, add property parser Example output for parse-props.py: download cable = 'USER' battery level = 100 download type = ' ' download speed = 0 usb version = ' ' hardware revision = 'rev_10' download sw version = ' ' device sw version = 'D85520t' secure device = 'S' laf sw version = '1.1' device factory version = 'LGD855AT-00-V20t-EUR-XX-AUG-18-2015-16G+0' device factory out version = 'LGD855AT-00-V20t-GLOBAL-COM-AUG-18-2015-16G+0' pid = 'YE01S151002002500' imei = (redacted) model name = 'LG-D855' device build type = 'U' chipset platform = 'msm8974' target_operator = 'GLOBAL' target_country = 'COM' ap_factory_reset_status = 3 cp_factory_reset_status = 0 isDownloadNotFinish = 0 qem = 0 cupss swfv = 'A1439892730-M1439892730-C1439940554-U1439940554-0' is one binary dual plan = 0 memory size = 30777344 memory_id = 'SDW16G\n' bootloader_ver = 'MiniOS 3.0' --- protocol.md | 62 ++++++++++++++++------- scripts/parse-props.py | 110 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 18 deletions(-) create mode 100755 scripts/parse-props.py diff --git a/protocol.md b/protocol.md index f82b27d..84ec614 100644 --- a/protocol.md +++ b/protocol.md @@ -34,12 +34,26 @@ error code and the original request header as body. ## Commands -### OPEN - Open -Arguments: none -Request body: at most 276 (0x114) bytes (?) +### OPEN - Open File +Opens a file path. -### CLSE - Close -Arguments: none +Arguments: + - arg1 (response): DWORD file descriptor. +Request body: NUL-terminated file path that should be opened for reading or an + empty string to open `/dev/block/mmcblk0` in read/write mode. +(at most 276 (0x114) bytes?) + +Non-existing files result in FAIL with error code 0x80000001. + +### CLSE - Close File +Closes a file descriptor which was returned by the `OPEN` command. + +Arguments: + - arg1: DWORD file descriptor (same in request and response). + +Note: this allows you to close any file descriptor that are in use by the `lafd` +process, not just the one returned by `OPEN`. You can discover the current file +descriptors via `ls -l /proc/$pid/fd` where `$pid` is found by `ps | grep lafd`. ### HELO - Hello Arguments: @@ -53,19 +67,28 @@ Arguments: Note: `CTRL(RSET)` with no body is sent by the `Send_Command.exe` utility for the `LEAVE` command. -### WRTE - Write -Arguments: - - arg1: ? - - arg2: ? +### WRTE - Write File +Writes to a file descriptor. -### READ - Read Arguments: - - arg1: ? - - arg2: ? - - arg3: ? -Response body: present. + - arg1: file descriptor (must be open for writing!) + - arg2: offset in **blocks** (multiple of 512 bytes). +Request body: the data to be written. Can be of any size (including 1 or 513). -(Arguments probably encode read offset, length) +Note: writing to a file descriptor which was opened for reading results in FAIL +with code 0x82000002. This command is likely used for writing to partitions. + +### READ - Read File +Reads from a file descriptor. + +Arguments: + - arg1: file descriptor + - arg2: offset in **blocks** (multiple of 512 bytes). + - arg3: requested length in bytes. +Response body: data in file at given offset and requested length. + +Note: be sure not to read past the end of the file (512 * offset + length), this +will hang the communication, requiring a reset (pull out battery)! ### ERSE - Erase Arguments: @@ -83,11 +106,14 @@ see standard error, use variables and globbing, use a command such as: sh -c "$@" -- eval 2>&1 echo $PATH -### INFO - Get Info +### INFO Arguments: - arg1: action (`GPRO` - Get Properties, `SPRO` - Set Properties) -Request body: fixed size 2824 (0xb08) -Response body: present for `GPRO` +Request body: must begin with two bytes (`08 0b`). +Response body: 2824 (0xb08) bytes of binary info. + +See [scripts/parse-props.py](scripts/parse-props.py) for the structure of the +property body. ### UNLK - Unlock Arguments: none diff --git a/scripts/parse-props.py b/scripts/parse-props.py new file mode 100755 index 0000000..e89c86e --- /dev/null +++ b/scripts/parse-props.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# Parse property file. +# +# Usage: +# +# lglaf.py -c '!INFO GPRO \x08\x0b' > props.bin +# scripts/parse-props.py props.bin + +import argparse, sys, struct + +def stringify(resp): + if not isinstance(resp, str): + try: resp = resp.decode('ascii') + except: pass + return resp + +def get_str(data, shadow, offset): + resp = b'' + #while True: + while offset < len(data): + b = data[offset:offset+1] + shadow[offset] = 's' + if b == b'\0': + break + resp += b + offset += 1 + return stringify(resp) + +def get_chr(data, shadow, offset): + b = data[offset:offset+1] + shadow[offset] = 'c' + return stringify(b) + +def get_int(data, shadow, offset): + d = struct.unpack_from('