Skip to content

Commit

Permalink
compress firmwares, reduce driver size
Browse files Browse the repository at this point in the history
zxystd committed Sep 4, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 29d5ed4 commit 81df136
Showing 9 changed files with 229 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -5,3 +5,4 @@ xcuserdata
xcshareddata
project.xcworkspace
FwBinary.cpp
*.pyc
28 changes: 28 additions & 0 deletions include/FwData.h
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@

#include <string.h>
#include <libkern/c++/OSData.h>
#include <libkern/zlib.h>
#include <zutil.h>

struct FwDesc {
const char *name;
@@ -41,4 +43,30 @@ static inline OSData *getFWDescByName(const char* name) {
return NULL;
}

static inline bool uncompressFirmware(unsigned char *dest, uint *destLen, unsigned char *source, uint sourceLen)
{
z_stream stream;
int err;

stream.next_in = source;
stream.avail_in = sourceLen;
stream.next_out = dest;
stream.avail_out = *destLen;
stream.zalloc = zcalloc;
stream.zfree = zcfree;
err = inflateInit(&stream);
if (err != Z_OK) {
return false;
}
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
return false;
}
*destLen = (uint)stream.total_out;

err = inflateEnd(&stream);
return err == Z_OK;
}

#endif /* FwData_h */
40 changes: 40 additions & 0 deletions itl80211/zutil.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// zutil.c
// itlwm
//
// Created by qcwap on 2020/9/4.
// Copyright © 2020 钟先耀. All rights reserved.
//

#include "zutil.h"
extern "C" {
typedef struct z_mem
{
UInt32 alloc_size;
UInt8 data[0];
} z_mem;

void *zcalloc(void *opaque, uint items, uint size)
{
void* result = NULL;
z_mem* zmem = NULL;
UInt32 allocSize = items * size + sizeof(zmem);

zmem = (z_mem*)IOMalloc(allocSize);

if (zmem)
{
zmem->alloc_size = allocSize;
result = (void*)&(zmem->data);
}

return result;
}

void zcfree(void *opaque, void *ptr)
{
UInt32* skipper = (UInt32 *)ptr - 1;
z_mem* zmem = (z_mem*)skipper;
IOFree((void*)zmem, zmem->alloc_size);
}
}
21 changes: 21 additions & 0 deletions itl80211/zutil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// zutil.h
// itlwm
//
// Created by qcwap on 2020/9/4.
// Copyright © 2020 钟先耀. All rights reserved.
//

#ifndef zutil_h
#define zutil_h

#include <IOKit/IOLib.h>
#include <IOKit/IOTypes.h>

extern "C" {
void *zcalloc(void *opaque, uint items, uint size);

void zcfree(void *opaque, void *ptr);
}

#endif /* zutil_h */
18 changes: 13 additions & 5 deletions itlwm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -135,12 +135,14 @@
F8F92583240974EF0088B8D5 /* random.h in Headers */ = {isa = PBXBuildFile; fileRef = F8F92578240974EE0088B8D5 /* random.h */; };
F8F92586240974EF0088B8D5 /* types.h in Headers */ = {isa = PBXBuildFile; fileRef = F8F9257B240974EF0088B8D5 /* types.h */; };
F8F9EDE1240B7415009CB8E7 /* _task.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F8F9EDE0240B7415009CB8E7 /* _task.cpp */; };
F8FA0EEC2501E7EE00B1822E /* zutil.h in Headers */ = {isa = PBXBuildFile; fileRef = F8FA0EEB2501E7EE00B1822E /* zutil.h */; };
F8FA0EEE2501E8C100B1822E /* zutil.c in Sources */ = {isa = PBXBuildFile; fileRef = F8FA0EED2501E8C100B1822E /* zutil.c */; };
F8FC31CF247E797E00FB456B /* itlwm_interface.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F8FC31CD247E797E00FB456B /* itlwm_interface.cpp */; };
F8FC31D0247E797E00FB456B /* itlwm_interface.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F8FC31CE247E797E00FB456B /* itlwm_interface.hpp */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
F8585CC42500EDDB00485C5A /* PBXContainerItemProxy */ = {
F8585CC62501E05100485C5A /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 024A07A323FCBC3C009FBA6C /* Project object */;
proxyType = 1;
@@ -309,6 +311,8 @@
F8F9257B240974EF0088B8D5 /* types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = types.h; sourceTree = "<group>"; };
F8F9EDDF240B4741009CB8E7 /* _task.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = _task.h; sourceTree = "<group>"; };
F8F9EDE0240B7415009CB8E7 /* _task.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = _task.cpp; sourceTree = "<group>"; };
F8FA0EEB2501E7EE00B1822E /* zutil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = zutil.h; sourceTree = "<group>"; };
F8FA0EED2501E8C100B1822E /* zutil.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = zutil.c; sourceTree = "<group>"; };
F8FC31CD247E797E00FB456B /* itlwm_interface.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = itlwm_interface.cpp; sourceTree = "<group>"; };
F8FC31CE247E797E00FB456B /* itlwm_interface.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = itlwm_interface.hpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -583,6 +587,8 @@
024A07C723FCBC6C009FBA6C /* openbsd */,
024A07BA23FCBC6C009FBA6C /* compat.cpp */,
024A082123FCBC6C009FBA6C /* compat.h */,
F8FA0EEB2501E7EE00B1822E /* zutil.h */,
F8FA0EED2501E8C100B1822E /* zutil.c */,
F8C2EC9D24080796007A9422 /* libkmod.a */,
F8D6CD502442E0D100D2A454 /* itl80211.hpp */,
F8D6CD522442E0D100D2A454 /* itl80211.cpp */,
@@ -669,6 +675,7 @@
F8C2EC632408031A007A9422 /* ieee80211.h in Headers */,
024A085623FCBC6C009FBA6C /* cmac.h in Headers */,
F8F9257E240974EF0088B8D5 /* bitfield.h in Headers */,
F8FA0EEC2501E7EE00B1822E /* zutil.h in Headers */,
F8C2EC642408031A007A9422 /* ieee80211_priv.h in Headers */,
024A07B023FCBC3C009FBA6C /* itlwm.hpp in Headers */,
F8C2EC5A2408031A007A9422 /* ieee80211_crypto.h in Headers */,
@@ -682,7 +689,7 @@
/* Begin PBXLegacyTarget section */
F8EE4D1B24178BF0002FA666 /* fw_gen */ = {
isa = PBXLegacyTarget;
buildArgumentsString = "-P ${PROJECT_DIR}/itlwm/firmware/*";
buildArgumentsString = "-P ${PROJECT_DIR}/itlwm/firmware/";
buildConfigurationList = F8EE4D1C24178BF0002FA666 /* Build configuration list for PBXLegacyTarget "fw_gen" */;
buildPhases = (
);
@@ -709,7 +716,7 @@
buildRules = (
);
dependencies = (
F8585CC52500EDDB00485C5A /* PBXTargetDependency */,
F8585CC72501E05100485C5A /* PBXTargetDependency */,
);
name = itlwm;
productName = itlwm;
@@ -792,6 +799,7 @@
F8D364F824F93AFD0029340B /* ItlHalService.cpp in Sources */,
F8C2EC6C2408031A007A9422 /* ieee80211_node.c in Sources */,
024A08C623FCDC3B009FBA6C /* tx.cpp in Sources */,
F8FA0EEE2501E8C100B1822E /* zutil.c in Sources */,
024A083423FCBC6C009FBA6C /* cast.c in Sources */,
024A08CA23FCE537009FBA6C /* phy.cpp in Sources */,
F8C2EC502408031A007A9422 /* _string.c in Sources */,
@@ -841,10 +849,10 @@
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
F8585CC52500EDDB00485C5A /* PBXTargetDependency */ = {
F8585CC72501E05100485C5A /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = F8EE4D1B24178BF0002FA666 /* fw_gen */;
targetProxy = F8585CC42500EDDB00485C5A /* PBXContainerItemProxy */;
targetProxy = F8585CC62501E05100485C5A /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

7 changes: 4 additions & 3 deletions itlwm/hal_iwm/fw.cpp
Original file line number Diff line number Diff line change
@@ -267,11 +267,12 @@ iwm_read_firmware(struct iwm_softc *sc, enum iwm_ucode_type ucode_type)
fwData = getFWDescByName(sc->sc_fwname);
if (fwData == NULL) {
XYLog("%s resource load fail.\n", sc->sc_fwname);
err = EINVAL;
goto out;
}
fw->fw_rawdata = malloc(fwData->getLength(), 1, 1);
memcpy(fw->fw_rawdata, (u_char*)fwData->getBytesNoCopy(), fwData->getLength());
fw->fw_rawsize = fwData->getLength();
fw->fw_rawsize = fwData->getLength() * 4;
fw->fw_rawdata = malloc(fw->fw_rawsize, 1, 1);
uncompressFirmware((u_char *)fw->fw_rawdata, (uint *)&fw->fw_rawsize, (u_char *)fwData->getBytesNoCopy(), fwData->getLength());
XYLog("load firmware %s done\n", sc->sc_fwname);
sc->sc_capaflags = 0;
sc->sc_capa_n_scan_channels = IWM_DEFAULT_SCAN_CHANNELS;
7 changes: 4 additions & 3 deletions itlwm/hal_iwx/ItlIwx.cpp
Original file line number Diff line number Diff line change
@@ -897,12 +897,13 @@ iwx_read_firmware(struct iwx_softc *sc)
// fw->fw_rawsize = context.resource->getLength();
fwData = getFWDescByName(sc->sc_fwname);
if (fwData == NULL) {
err = EINVAL;
XYLog("%s resource load fail.\n", sc->sc_fwname);
goto out;
}
fw->fw_rawdata = malloc(fwData->getLength(), 1, 1);
memcpy(fw->fw_rawdata, (u_char*)fwData->getBytesNoCopy(), fwData->getLength());
fw->fw_rawsize = fwData->getLength();
fw->fw_rawsize = fwData->getLength() * 4;
fw->fw_rawdata = malloc(fw->fw_rawsize, 1, 1);
uncompressFirmware((u_char *)fw->fw_rawdata, (uint *)&fw->fw_rawsize, (u_char *)fwData->getBytesNoCopy(), fwData->getLength());
XYLog("load firmware %s done\n", sc->sc_fwname);

sc->sc_capaflags = 0;
21 changes: 21 additions & 0 deletions scripts/fw_gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

# fw_gen.sh
# itlwm
#
# Created by qcwap on 2020/3/10.
# Copyright © 2020 钟先耀. All rights reserved.
target_file="${PROJECT_DIR}/include/FwBinary.cpp"
while [ $# -gt 0 ];
do
case $1 in
-P) fw_files=$2
shift
;;

esac
shift
done

script_file="${PROJECT_DIR}/scripts/"
python -c 'import sys;sys.path.append("'$script_file'");from zlib_compress_fw import *;process_files("'${target_file}'", "'$fw_files'")'
97 changes: 97 additions & 0 deletions scripts/zlib_compress_fw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# zlib_compress_fw.py
#
# Created by qcwap on 2020/9/3.
# Copyright © 2020 钟先耀. All rights reserved.

import zlib
import os
import struct

copyright = '''
// itlwm
//
// Copyright © 2020 钟先耀. All rights reserved.
#include "FwData.h"
'''

def compress(data):
return zlib.compress(data)

def format_file_name(file_name):
return file_name.replace(".", "_").replace("-", "_")

def write_single_file(target_file, path, file):
src_file = open(path, "rb")
src_data = src_file.read()
src_data = compress(src_data)
src_len = len(src_data)

fw_var_name = format_file_name(file)
target_file.write("\nconst unsigned char ")
target_file.write(fw_var_name)
target_file.write("[] = {")
index = 0;
block = []
while True:
if index + 16 >= src_len:
block = src_data[index:]
else:
block = src_data[index:index + 16]
index += 16;
if len(block) < 16:
if len(block):
for b in block:
if type(b) is str:
b = ord(b)
target_file.write("0x{:02X}, ".format(b))
target_file.write("\n")
break
target_file.write("0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, "
"0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, "
"0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X}, "
"0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X},\n"
.format(*struct.unpack("BBBBBBBBBBBBBBBB", block)))
target_file.write("};\n")
target_file.write("const long int ")
target_file.write(fw_var_name)
target_file.write("_size = sizeof(")
target_file.write(fw_var_name)
target_file.write(");\n")
src_file.close()


def process_files(target_file, dir):
if not os.path.exists(target_file):
if not os.path.exists(os.path.dirname(target_file)):
os.mkdirs(os.path.dirname(target_file))
target_file_handle = open(target_file, "w")
target_file_handle.write(copyright)
for root, dirs, files in os.walk(dir):
for file in files:
path = os.path.join(root, file)
write_single_file(target_file_handle, path, file)

target_file_handle.write("\n")
target_file_handle.write("const struct FwDesc fwList[] = {")

for file in files:
target_file_handle.write('{IWL_FW("')
target_file_handle.write(file)
target_file_handle.write('", ')
fw_var_name = format_file_name(file)
target_file_handle.write(fw_var_name)
target_file_handle.write(", ")
target_file_handle.write(fw_var_name)
target_file_handle.write("_size)},\n")

target_file_handle.write("};\n")
target_file_handle.write("const int fwNumber = ")
target_file_handle.write(str(len(files)))
target_file_handle.write(";\n")

target_file_handle.close()

if __name__ == '__main__':
print compress("test");

0 comments on commit 81df136

Please sign in to comment.