int set_color(int led, int intensity, int r, int g, int b) { uint16_t color = ((r)+((g)<<4)+((b)<<8)); int i; write_begin(); for (i=6; i; i--, (led <<= 1)) { if (led&(1<<5)) { write_one(); } else { write_zero(); } } for (i=8; i; i--, (intensity<<=1)) { if (intensity&(1<<7)) { write_one(); } else { write_zero(); } } for (i=12; i; i--, (color<<=1)) { if (color&(1<<11)) { write_one(); } else { write_zero(); } } write_end(); return 0; }
int split_fill(split_info_t *s, int idx, u64 size) { int fd = split_open_file(s, idx); off64_t fsize = lseek(fd, 0, SEEK_END); if (fsize < size) { //printf("TRUNC %d "FMT_lld"\n", idx, size); Wpad_WaitButtons(); //ftruncate(fd, size); write_zero(fd, size - fsize); return 1; } return 0; }
int main(void) { while (1) { refresh_inputs(); if (isSchedulable_done()) { done(); } else if (isSchedulable_write_value()) { int ports = 0; if (!FIFO_HAS_ROOM(decoder_parser_blkexp_OUT)(&fifo_o_decoder_parser_blkexp_OUT, 1)) { ports |= 0x01; } if (ports != 0) { continue; } write_value(); } else if (isSchedulable_write_zero()) { int ports = 0; if (!FIFO_HAS_ROOM(decoder_parser_blkexp_OUT)(&fifo_o_decoder_parser_blkexp_OUT, 1)) { ports |= 0x01; } if (ports != 0) { continue; } write_zero(); } else if ((decoder_parser_blkexp_RUN_tokens >= 1) && (decoder_parser_blkexp_VALUE_tokens >= 1) && (decoder_parser_blkexp_LAST_tokens >= 1) && isSchedulable_read_immediate()) { int ports = 0; if (!FIFO_HAS_ROOM(decoder_parser_blkexp_OUT)(&fifo_o_decoder_parser_blkexp_OUT, 1)) { ports |= 0x01; } if (ports != 0) { continue; } read_immediate(); } else if ((decoder_parser_blkexp_RUN_tokens >= 1) && (decoder_parser_blkexp_VALUE_tokens >= 1) && (decoder_parser_blkexp_LAST_tokens >= 1) && isSchedulable_read_save()) { int ports = 0; if (!FIFO_HAS_ROOM(decoder_parser_blkexp_OUT)(&fifo_o_decoder_parser_blkexp_OUT, 1)) { ports |= 0x01; } if (ports != 0) { continue; } read_save(); } else { continue; } } }
void onewire_write_byte (uint8_t c) { uint8_t i; uint8_t mask; #ifdef DBG_ONEWIRE OW_Bus_PORT |= _BV(1); #endif for (mask=1, i=0; i < 8; mask <<= 1, i++) if ((c & mask)) write_one (); else write_zero (); _delay_us (20); #ifdef DBG_ONEWIRE OW_Bus_PORT &= ~_BV(1); #endif }
int main(void) { DECLARE_HDR(hdr); char boot_program[MAX_BOOT_PROG_LEN]; size_t aligned_hdr_len, alligned_prog_len; ssize_t prog_len; prog_len = do_read(0, boot_program, sizeof(boot_program)); if (prog_len <= 0) return -1; aligned_hdr_len = ROUND_UP(sizeof(hdr)); hdr.start_address = htole32(START_BASE + aligned_hdr_len); alligned_prog_len = ROUND_UP(prog_len); hdr.copy_size = htole16(aligned_hdr_len + alligned_prog_len); if (do_write(1, &hdr, sizeof(hdr)) < 0) return -1; if (write_zero(1, aligned_hdr_len - sizeof(hdr)) < 0) return -1; if (do_write(1, boot_program, prog_len) < 0) return 1; /* Write out the rest of the kernel */ while (1) { prog_len = do_read(0, boot_program, sizeof(boot_program)); if (prog_len < 0) return 1; if (prog_len == 0) break; if (do_write(1, boot_program, prog_len) < 0) return 1; } return 0; }
void write_aout(struct aout *a, const char *buf, int len, const char *src) { char *file, *p; FILE *f; if (a->textlen == 0) return; p = strrchr(src, '.'); if (p == NULL) { printf("INVALID FILE NAME: %s\n", src); return; } file = strdup(src); *strrchr(file, '.') = '\0'; printf("convert PE to a.out: %s => %s\n", src, file); f = fopen(file, "wb"); if (f == NULL) printf("CAN NOT WRITE\n"); else { int entry = a->header.a_entry; if (a->textvad > 10) a->header.a_entry = 0; fwrite(&a->header, sizeof(a->header), 1, f); if (a->textvad > 10) { char jmp[32]; short *p_magic = (short *)&buf[a->datapos]; jmp[0] = 0xeb; // jmp short jmp[1] = 5; *(short *)&jmp[2] = click_shift; *(short *)&jmp[4] = k_flags; jmp[6] = 0x90; // nop jmp[7] = 0xb8; // mov eax *(int *)&jmp[8] = a->header.a_text + a->header.a_data + a->header.a_bss; jmp[12] = 0xbb; // mov ebx *(int *)&jmp[13] = a->header.a_text; jmp[17] = 0xb9; // mov ecx *(int *)&jmp[18] = A_SYMPOS(a->header); jmp[22] = 0xba; // mov edx *(int *)&jmp[23] = *p_magic; jmp[27] = 0xe9; // jmp *(int *)&jmp[28] = entry - sizeof(jmp); fwrite(jmp, sizeof(jmp), 1, f); write_zero(f, a->textvad - sizeof(jmp)); *p_magic = magic; } fwrite(&buf[a->textpos], a->textlen, 1, f); write_zero(f, a->header.a_text - a->textlen - a->textvad); if (a->datalen > 0) { fwrite(&buf[a->datapos], a->datalen, 1, f); if (a->rdatalen == 0) write_zero(f, a->header.a_data - a->datalen); else write_zero(f, (a->rdatavad - a->datavad) - a->datalen); } if (a->rdatalen > 0) { fwrite(&buf[a->rdatapos], a->rdatalen, 1, f); if (a->datalen == 0) write_zero(f, a->header.a_data - a->rdatalen); else write_zero(f, a->header.a_data - a->rdatalen - (a->rdatavad - a->datavad)); } fclose(f); #ifndef WIN32 chmod(file, 0755); #endif } free(file); }
void repack(const char* orig_image, const char* out_image) { size_t size; unsigned char *orig; char name[PATH_MAX]; // There are possible two MTK headers mtk_hdr mtk_kernel_hdr, mtk_ramdisk_hdr; size_t mtk_kernel_off, mtk_ramdisk_off; // Load original image mmap_ro(orig_image, &orig, &size); // Parse original image printf("Parsing boot image: [%s]\n\n", orig_image); parse_img(orig, size); printf("Repack to boot image: [%s]\n\n", out_image); // Create new image int fd = open_new(out_image); // Set all sizes to 0 hdr.kernel_size = 0; hdr.ramdisk_size = 0; hdr.second_size = 0; hdr.dt_size = 0; // Skip a page for header write_zero(fd, hdr.page_size); // Restore kernel if (mtk_kernel) { mtk_kernel_off = lseek(fd, 0, SEEK_CUR); restore_buf(fd, kernel, 512); memcpy(&mtk_kernel_hdr, kernel, sizeof(mtk_kernel_hdr)); } hdr.kernel_size = restore(KERNEL_FILE, fd); file_align(fd, hdr.page_size, 1); // Restore ramdisk if (mtk_ramdisk) { mtk_ramdisk_off = lseek(fd, 0, SEEK_CUR); restore_buf(fd, ramdisk, 512); memcpy(&mtk_ramdisk_hdr, ramdisk, sizeof(mtk_ramdisk_hdr)); } if (access(RAMDISK_FILE, R_OK) == 0) { // If we found raw cpio, compress to original format // Before we start, clean up previous compressed files for (int i = 0; SUP_EXT_LIST[i]; ++i) { sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]); unlink(name); } size_t cpio_size; unsigned char *cpio; mmap_ro(RAMDISK_FILE, &cpio, &cpio_size); if (comp(ramdisk_type, RAMDISK_FILE, cpio, cpio_size)) LOGE(1, "Unsupported ramdisk format!\n"); munmap(cpio, cpio_size); } int found = 0; for (int i = 0; SUP_EXT_LIST[i]; ++i) { sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]); if (access(name, R_OK) == 0) { ramdisk_type = SUP_TYPE_LIST[i]; found = 1; break; } } if (!found) LOGE(1, "No ramdisk exists!\n"); hdr.ramdisk_size = restore(name, fd); file_align(fd, hdr.page_size, 1); // Restore second if (access(SECOND_FILE, R_OK) == 0) { hdr.second_size = restore(SECOND_FILE, fd); file_align(fd, hdr.page_size, 1); } // Restore dtb if (access(DTB_FILE, R_OK) == 0) { hdr.dt_size = restore(DTB_FILE, fd); file_align(fd, hdr.page_size, 1); } // Check extra info, currently only for LG Bump and Samsung SEANDROIDENFORCE if (extra) { if (memcmp(extra, "SEANDROIDENFORCE", 16) == 0 || memcmp(extra, "\x41\xa9\xe4\x67\x74\x4d\x1d\x1b\xa4\x29\xf2\xec\xea\x65\x52\x79", 16) == 0 ) { restore_buf(fd, extra, 16); } } // Write headers back if (mtk_kernel) { lseek(fd, mtk_kernel_off, SEEK_SET); mtk_kernel_hdr.size = hdr.kernel_size; hdr.kernel_size += 512; restore_buf(fd, &mtk_kernel_hdr, sizeof(mtk_kernel_hdr)); } if (mtk_ramdisk) { lseek(fd, mtk_ramdisk_off, SEEK_SET); mtk_ramdisk_hdr.size = hdr.ramdisk_size; hdr.ramdisk_size += 512; restore_buf(fd, &mtk_ramdisk_hdr, sizeof(mtk_ramdisk_hdr)); } // Main header lseek(fd, 0, SEEK_SET); restore_buf(fd, &hdr, sizeof(hdr)); // Print new image info print_info(); munmap(orig, size); close(fd); }