Skip to content

Instantly share code, notes, and snippets.

@binarycraft007
binarycraft007 / dtb-splitter.zig
Created May 24, 2025 15:29
A program to split dtb from appended kernel image
//! This program splits a kernel image file that has Device Tree Blobs (DTBs) appended to it.
//! It extracts the main kernel part and each DTB into separate files.
//! The kernel is saved as "kernel". DTBs are saved as "dtbdump_1.dtb", "dtbdump_2.dtb", etc.
const std = @import("std");
const fs = std.fs;
const mem = std.mem;
const heap = std.heap;
const fmt = std.fmt;
const process = std.process;
@binarycraft007
binarycraft007 / atcmd.zig
Created May 16, 2025 03:01
AT command tool in zig
const max_output_bytes = 4096;
pub fn main() !void {
var arena_state = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
var file = try std.fs.openFileAbsolute("/dev/smd7", .{ .mode = .read_write });
defer file.close();
@binarycraft007
binarycraft007 / mnl.zig
Created April 20, 2025 14:18
mnl in zig
const std = @import("std");
const mem = std.mem;
const linux = std.os.linux;
const posix = std.posix;
const assert = std.debug.assert;
const testing = std.testing;
const AttrDataType = enum(u14) {
unspec,
u8,
@binarycraft007
binarycraft007 / event_loop.zig
Created April 16, 2025 06:00
epoll event loop in zig
// Allocation-free event loop inspired by uloop: fixed arrays, no heap allocations.
const std = @import("std");
const mem = std.mem;
const posix = std.posix;
const linux = std.os.linux;
const testing = std.testing;
const log = std.log.scoped(.event_loop);
pub const MAX_EVENTS = 64;
pub const MAX_HANDLERS = 128;
const std = @import("std");
pub fn main() !void {
// Use the debug allocator.
var gpa_state: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa_state.deinit();
const gpa = gpa_state.allocator();
// Expect two arguments: <elf_file> and <prefix>
const args = try std.process.argsAlloc(gpa);
const file_version = &[_]u8{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
pub fn main() !void {
var gpa_state: std.heap.GeneralPurposeAllocator(.{}) = .init;
defer _ = gpa_state.deinit();
const gpa = gpa_state.allocator();
var raw = std.ArrayList(u8).init(gpa);
defer raw.deinit();
@binarycraft007
binarycraft007 / imei.zig
Created January 20, 2025 00:23
imei to raw hex
const std = @import("std");
const testing = std.testing;
const assert = std.debug.assert;
const IMEI = struct {
raw: [26]u8,
pub fn parse(imei_str: []const u8) !IMEI {
var imei: IMEI = undefined;
assert(imei_str.len == 15);
@binarycraft007
binarycraft007 / hdlc.zig
Created January 13, 2025 13:29
hdlc zig implementation
const std = @import("std");
const mem = std.mem;
const hdlc = @This();
const escape_char = '\x7d';
const trailer_char = '\x7e';
const Crc16Ccitt = std.hash.crc.Crc(u16, .{
.polynomial = 0x11021,
.initial = 0x0000,
@binarycraft007
binarycraft007 / hdlc.c
Created January 13, 2025 09:17
Implements the pseudo-HDLC framing using for the Qualcomm Diag protocol.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
// Constants for HDLC framing
#define ESCAPE_CHAR 0x7D
#define TRAILER_CHAR 0x7E
@binarycraft007
binarycraft007 / libserialport.c
Created January 12, 2025 11:58
libserialport example
#include <stdio.h>
#include <libserialport.h>
int main() {
struct sp_port *port;
enum sp_return result;
// Open the serial port
const char *port_name = "/dev/ttyS0"; // Replace with your serial port name
result = sp_get_port_by_name(port_name, &port);