static void add_label(struct brw_program_instruction *i) { struct label_item **p = &label_table; assert(is_label(i)); while(*p) p = &((*p)->next); *p = calloc(1, sizeof(**p)); (*p)->name = label_name(i); (*p)->addr = i->inst_offset; }
void mlabel(int argc, char **argv, int type) { char *newLabel; int verbose, clear, interactive, show; direntry_t entry; int result=0; char longname[VBUFSIZE]; char shortname[45]; ClashHandling_t ch; struct MainParam_t mp; Stream_t *RootDir; int c; int mangled; enum { SER_NONE, SER_RANDOM, SER_SET } set_serial = SER_NONE; long serial = 0; int need_write_boot = 0; int have_boot = 0; char *eptr; union bootsector boot; Stream_t *Fs=0; int r; struct label_blk_t *labelBlock; int isRo=0; int *isRop=NULL; init_clash_handling(&ch); ch.name_converter = label_name; ch.ignore_entry = -2; verbose = 0; clear = 0; show = 0; if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:vcsnN:h")) != EOF) { switch (c) { case 'i': set_cmd_line_image(optarg, 0); break; case 'v': verbose = 1; break; case 'c': clear = 1; break; case 's': show = 1; break; case 'n': set_serial = SER_RANDOM; srandom((long)time (0)); serial=random(); break; case 'N': set_serial = SER_SET; serial = strtol(optarg, &eptr, 16); if(*eptr) { fprintf(stderr, "%s not a valid serial number\n", optarg); exit(1); } break; case 'h': usage(0); default: usage(1); } } if (argc - optind != 1 || !argv[optind][0] || argv[optind][1] != ':') usage(1); init_mp(&mp); newLabel = argv[optind]+2; if(strlen(newLabel) > VBUFSIZE) { fprintf(stderr, "Label too long\n"); FREE(&RootDir); exit(1); } interactive = !show && !clear &&!newLabel[0] && (set_serial == SER_NONE); if(!clear && !newLabel[0]) { isRop = &isRo; } RootDir = open_root_dir(argv[optind][0], isRop ? 0 : O_RDWR, isRop); if(isRo) { show = 1; interactive = 0; } if(!RootDir) { fprintf(stderr, "%s: Cannot initialize drive\n", argv[0]); exit(1); } initializeDirentry(&entry, RootDir); r=vfat_lookup(&entry, 0, 0, ACCEPT_LABEL | MATCH_ANY, shortname, longname); if (r == -2) { FREE(&RootDir); exit(1); } if(show || interactive){ if(isNotFound(&entry)) printf(" Volume has no label\n"); else if (*longname) printf(" Volume label is %s (abbr=%s)\n", longname, shortname); else printf(" Volume label is %s\n", shortname); } /* ask for new label */ if(interactive){ newLabel = longname; fprintf(stderr,"Enter the new volume label : "); if(fgets(newLabel, VBUFSIZE, stdin) == NULL) { newLabel[0] = '\0'; fprintf(stderr, "\n"); } if(newLabel[0]) newLabel[strlen(newLabel)-1] = '\0'; } if((!show || newLabel[0]) && !isNotFound(&entry)){ /* if we have a label, wipe it out before putting new one */ if(interactive && newLabel[0] == '\0') if(ask_confirmation("Delete volume label (y/n): ")){ FREE(&RootDir); exit(0); } entry.dir.attr = 0; /* for old mlabel */ wipeEntry(&entry); } if (newLabel[0] != '\0') { ch.ignore_entry = 1; result = mwrite_one(RootDir,newLabel,0,labelit,NULL,&ch) ? 0 : 1; } have_boot = 0; if( (!show || newLabel[0]) || set_serial != SER_NONE) { Fs = GetFs(RootDir); have_boot = (force_read(Fs,boot.characters,0,sizeof(boot)) == sizeof(boot)); } if(WORD_S(fatlen)) { labelBlock = &boot.boot.ext.old.labelBlock; } else { labelBlock = &boot.boot.ext.fat32.labelBlock; } if(!show || newLabel[0]){ dos_name_t dosname; const char *shrtLabel; doscp_t *cp; if(!newLabel[0]) shrtLabel = "NO NAME "; else shrtLabel = newLabel; cp = GET_DOSCONVERT(Fs); label_name(cp, shrtLabel, verbose, &mangled, &dosname); if(have_boot && boot.boot.descr >= 0xf0 && labelBlock->dos4 == 0x29) { strncpy(labelBlock->label, dosname.base, 11); need_write_boot = 1; } } if((set_serial != SER_NONE) & have_boot) { if(have_boot && boot.boot.descr >= 0xf0 && labelBlock->dos4 == 0x29) { set_dword(labelBlock->serial, serial); need_write_boot = 1; } } if(need_write_boot) { force_write(Fs, (char *)&boot, 0, sizeof(boot)); } FREE(&RootDir); exit(result); }
// Call back for querying the text of a // menu item, or menu. // Expect item names like /1 & /3/4, and // Menu names like / & /3/ static char *task_cb_item_text(char *name) { // Deconstruct the menu ID APP_LOG(APP_LOG_LEVEL_DEBUG, "task_cb_item_text(%s)", name); uint8_t parts[MAX_MENU_DEPTH]; int num_parts = menu_parts(name, parts); char *rc = NULL; // It's a menu if the ID ends with a '/' bool is_menu = (name[strlen(name)-1] == '/'); if (num_parts == 0) { rc = "Categories"; } else if ((num_parts == 1) && (parts[0] == 1)) { rc = "Recent"; } else { // Get the label ID uint8_t label_id = 0; if (parts[0] != 1) { uint8_t *labels = ordered_labels(); label_id = labels[parts[0]-2]; APP_LOG(APP_LOG_LEVEL_DEBUG, "task_cb_item_text: Translate menu %d to label ID %d", parts[0], label_id); // If we're looking for a label name, set it up if (num_parts == 1) { APP_LOG(APP_LOG_LEVEL_DEBUG, "task_cb_item_text: Get label name for label ID %d", label_id); rc = label_name(label_id); } } // Handle the "Pause" at the top of the Recents menu uint8_t task_ix = parts[1] - 1; // Because menu-IDs are 1-based if (!g_menu_paused && (rc == NULL)) { APP_LOG(APP_LOG_LEVEL_DEBUG, "task_cb_item_text: Get task name"); if (parts[0] == 1) { // Recents menu - ignore the pause option if (task_ix == 0) { rc = "Pause"; } else { task_ix -= 1; // To make space for the pause option } } } // Get the task name from the ordered tasks if (rc == NULL) { uint8_t *tasks = ordered_tasks(label_id); uint8_t task_id = tasks[task_ix]; rc = task_name(task_id); } } if (rc == NULL) { rc = "Unknown"; } if (strlen(rc) >= MAX_MENU_ITEM_LEN) { APP_LOG(APP_LOG_LEVEL_DEBUG, "task_cb_item_text: dumping %s: too long", rc); rc = "Too long"; } APP_LOG(APP_LOG_LEVEL_DEBUG, "task_cb_item_text: return %s", rc); return rc; }
int fatlabel_set_label(const char* device_name, const char* new_label) { if (strlen(new_label) > VBUFSIZE) return -1; /* * 1. Init clash handling */ struct ClashHandling_t ch; init_clash_handling(&ch); ch.name_converter = label_name; ch.ignore_entry = -2; /* * 2. Open root dir */ struct Stream_t* RootDir = fs_init(device_name, O_RDWR); if (RootDir) RootDir = OpenRoot(RootDir); if (!RootDir) { fprintf(stderr, "Opening root dir failed.\n"); return -2; } /* * 3. Init dir entry */ struct direntry_t entry; initializeDirentry(&entry, RootDir); /* * 4. Lookup vfat */ char longname[VBUFSIZE]; char shortname[45]; if (vfat_lookup(&entry, 0, 0, ACCEPT_LABEL | MATCH_ANY, shortname, longname) == -2) { fprintf(stderr, "Looking up vfat failed.\n"); free_stream(&RootDir); return -3; } /* * 5. Wipe existing entry. */ if (!isNotFound(&entry)) { /* if we have a label, wipe it out before putting new one */ entry.dir.attr = 0; /* for old mlabel */ wipeEntry(&entry); } /* * 6. Write new entry */ ch.ignore_entry = 1; /* don't try to write the label if it's empty */ int result = strlen(new_label) ? mwrite_one(RootDir, new_label, labelit, &ch) : 0; /* * 7. Load FAT boot record */ union bootsector boot; struct Stream_t* Fs = GetFs(RootDir); int have_boot = force_read(Fs, boot.characters, 0, sizeof(boot)) == sizeof(boot); struct label_blk_t* labelBlock = WORD_S(fatlen) ? &boot.boot.ext.old.labelBlock : &boot.boot.ext.fat32.labelBlock; /* * 8. Get "dosconvert" struct */ struct dos_name_t dosname; struct doscp_t* cp = GET_DOSCONVERT(Fs); /* * 9. Convert label */ int mangled = 0; label_name(cp, new_label, &mangled, &dosname); /* * 10. Overwrite FAT boot record */ if (have_boot && boot.boot.descr >= 0xf0 && labelBlock->dos4 == 0x29) { strncpy(labelBlock->label, dosname.base, 11); force_write(Fs, (char *)&boot, 0, sizeof(boot)); } free_stream(&RootDir); fs_close(Fs); return result; }
int main(int argc, char **argv) { char *output_file = NULL; char *entry_table_file = NULL; FILE *output = stdout; FILE *export_file; struct brw_program_instruction *entry, *entry1, *tmp_entry; int err, inst_offset; char o; void *mem_ctx; while ((o = getopt_long(argc, argv, "e:l:o:g:abW", longopts, NULL)) != -1) { switch (o) { case 'o': if (strcmp(optarg, "-") != 0) output_file = optarg; break; case 'g': { char *dec_ptr, *end_ptr; unsigned long decimal; gen_level = strtol(optarg, &dec_ptr, 10) * 10; if (*dec_ptr == '.') { decimal = strtoul(++dec_ptr, &end_ptr, 10); if (end_ptr != dec_ptr && *end_ptr == '\0') { if (decimal > 10) { fprintf(stderr, "Invalid Gen X decimal version\n"); exit(1); } gen_level += decimal; } } if (gen_level < 40 || gen_level > 90) { usage(); exit(1); } break; } case 'a': advanced_flag = 1; break; case 'b': binary_like_output = 1; break; case 'e': need_export = 1; if (strcmp(optarg, "-") != 0) export_filename = optarg; break; case 'l': if (strcmp(optarg, "-") != 0) entry_table_file = optarg; break; case 'W': warning_flags |= WARN_ALL; break; default: usage(); exit(1); } } argc -= optind; argv += optind; if (argc != 1) { usage(); exit(1); } if (strcmp(argv[0], "-") != 0) { input_filename = argv[0]; yyin = fopen(input_filename, "r"); if (yyin == NULL) { perror("Couldn't open input file"); exit(1); } } brw_init_context(&genasm_brw_context, gen_level); mem_ctx = ralloc_context(NULL); brw_init_compile(&genasm_brw_context, &genasm_compile, mem_ctx); err = yyparse(); if (strcmp(argv[0], "-")) fclose(yyin); yylex_destroy(); if (err || errors) exit (1); if (output_file) { output = fopen(output_file, "w"); if (output == NULL) { perror("Couldn't open output file"); exit(1); } } if (read_entry_file(entry_table_file)) { fprintf(stderr, "Read entry file error\n"); exit(1); } inst_offset = 0 ; for (entry = compiled_program.first; entry != NULL; entry = entry->next) { entry->inst_offset = inst_offset; entry1 = entry->next; if (entry1 && is_label(entry1) && is_entry_point(entry1)) { // insert NOP instructions until (inst_offset+1) % 4 == 0 while (((inst_offset+1) % 4) != 0) { tmp_entry = calloc(sizeof(*tmp_entry), 1); tmp_entry->insn.gen.header.opcode = BRW_OPCODE_NOP; entry->next = tmp_entry; tmp_entry->next = entry1; entry = tmp_entry; tmp_entry->inst_offset = ++inst_offset; } } if (!is_label(entry)) inst_offset++; } for (entry = compiled_program.first; entry; entry = entry->next) if (is_label(entry)) add_label(entry); if (need_export) { if (export_filename) { export_file = fopen(export_filename, "w"); } else { export_file = fopen("export.inc", "w"); } for (entry = compiled_program.first; entry != NULL; entry = entry->next) { if (is_label(entry)) fprintf(export_file, "#define %s_IP %d\n", label_name(entry), (IS_GENx(5) ? 2 : 1)*(entry->inst_offset)); } fclose(export_file); } for (entry = compiled_program.first; entry; entry = entry->next) { struct relocation *reloc = &entry->reloc; if (!is_relocatable(entry)) continue; if (reloc->first_reloc_target) reloc->first_reloc_offset = label_to_addr(reloc->first_reloc_target, entry->inst_offset) - entry->inst_offset; if (reloc->second_reloc_target) reloc->second_reloc_offset = label_to_addr(reloc->second_reloc_target, entry->inst_offset) - entry->inst_offset; if (reloc->second_reloc_offset) { // this is a branch instruction with two offset arguments set_branch_two_offsets(entry, reloc->first_reloc_offset, reloc->second_reloc_offset); } else if (reloc->first_reloc_offset) { set_branch_one_offset(entry, reloc->first_reloc_offset); } } if (binary_like_output) fprintf(output, "%s", binary_prepend); for (entry = compiled_program.first; entry != NULL; entry = entry1) { entry1 = entry->next; if (!is_label(entry)) print_instruction(output, &entry->insn.gen); else free(entry->insn.label.name); free(entry); } if (binary_like_output) fprintf(output, "};"); free_entry_point_table(entry_point_table); free_hash_table(declared_register_table); free_label_table(label_table); fflush (output); if (ferror (output)) { perror ("Could not flush output file"); if (output_file) unlink (output_file); err = 1; } return err; }