static unsigned int get_hw_revision(void) { int hwrev, mode0, mode1; adc_power_control(1); mode0 = get_adc_value(1); /* HWREV_MODE0 */ mode1 = get_adc_value(2); /* HWREV_MODE1 */ /* * XXX Always set the default hwrev as the latest board * ADC = (voltage) / 3.3 * 4096 */ hwrev = 3; #define IS_RANGE(x, min, max) ((x) > (min) && (x) < (max)) if (IS_RANGE(mode0, 80, 200) && IS_RANGE(mode1, 80, 200)) hwrev = 0x0; /* 0.01V 0.01V */ if (IS_RANGE(mode0, 750, 1000) && IS_RANGE(mode1, 80, 200)) hwrev = 0x1; /* 610mV 0.01V */ if (IS_RANGE(mode0, 1300, 1700) && IS_RANGE(mode1, 80, 200)) hwrev = 0x2; /* 1.16V 0.01V */ if (IS_RANGE(mode0, 2000, 2400) && IS_RANGE(mode1, 80, 200)) hwrev = 0x3; /* 1.79V 0.01V */ #undef IS_RANGE debug("mode0: %d, mode1: %d, hwrev 0x%x\n", mode0, mode1, hwrev); adc_power_control(0); return hwrev; }
bool validateKey(WrenVM* vm, Value arg) { if (IS_BOOL(arg) || IS_CLASS(arg) || IS_FIBER(arg) || IS_NULL(arg) || IS_NUM(arg) || IS_RANGE(arg) || IS_STRING(arg)) { return true; } vm->fiber->error = CONST_STRING(vm, "Key must be a value type or fiber."); return false; }
bool validateKey(WrenVM* vm, Value* args, int index) { Value arg = args[index]; if (IS_BOOL(arg) || IS_CLASS(arg) || IS_NULL(arg) || IS_NUM(arg) || IS_RANGE(arg) || IS_STRING(arg)) { return true; } args[0] = CONST_STRING(vm, "Key must be a value type."); return false; }
static int process_read_dir_entry_cmd(client_t *client, netiso_read_dir_entry_cmd *cmd, int version) { char *path; file_stat_t st; struct dirent *entry; size_t d_name_len = 0; netiso_read_dir_entry_result result_v1; netiso_read_dir_entry_result_v2 result_v2; if(version == 1) { memset(&result_v1, 0, sizeof(result_v1)); } else { memset(&result_v2, 0, sizeof(result_v2)); } if(!client->dir || !client->dirpath) { if(version == 1) { result_v1.file_size = BE64(-1); } else { result_v2.file_size = BE64(-1); } goto send_result_read_dir; } while ((entry = readdir(client->dir))) { if(IS_PARENT_DIR(entry->d_name)) continue; d_name_len = strlen(entry->d_name); if(IS_RANGE(d_name_len, 1, 65535)) break; } if(!entry) { closedir(client->dir); if(client->dirpath) free(client->dirpath); client->dir = NULL; client->dirpath = NULL; if(version == 1) { result_v1.file_size = BE64(-1); } else { result_v2.file_size = BE64(-1); } goto send_result_read_dir; } path = (char *)malloc(strlen(client->dirpath) + d_name_len + 2); if(!path) { DPRINTF("CRITICAL: memory allocation error\n"); goto send_result_read_dir; } sprintf(path, "%s/%s", client->dirpath, entry->d_name); DPRINTF("Read dir entry: %s\n", path); if(stat_file(path, &st) < 0) { closedir(client->dir); if(client->dirpath) free(client->dirpath); client->dir = NULL; client->dirpath = NULL; if(version == 1) { result_v1.file_size = BE64(-1); } else { result_v2.file_size = BE64(-1); } DPRINTF("Stat failed on read dir entry: %s\n", path); goto send_result_read_dir; } if((st.mode & S_IFDIR) == S_IFDIR) { if(version == 1) { result_v1.file_size = BE64(0); result_v1.is_directory = 1; } else { result_v2.file_size = BE64(0); result_v2.is_directory = 1; } } else { if(version == 1) { result_v1.file_size = BE64(st.file_size); result_v1.is_directory = 0; } else { result_v2.file_size = BE64(st.file_size); result_v2.is_directory = 0; } } if(version == 1) { result_v1.fn_len = BE16(d_name_len); } else { result_v2.fn_len = BE16(d_name_len); result_v2.atime = BE64(st.atime); result_v2.ctime = BE64(st.ctime); result_v2.mtime = BE64(st.mtime); } send_result_read_dir: if(path) free(path); if(version == 1) { if(send(client->s, (char *)&result_v1, sizeof(result_v1), 0) != sizeof(result_v1)) { DPRINTF("send error on read dir entry (%d)\n", get_network_error()); return -1; } } else { if(send(client->s, (char *)&result_v2, sizeof(result_v2), 0) != sizeof(result_v2)) { DPRINTF("send error on read dir entry (%d)\n", get_network_error()); return -1; } } if((version == 1 && result_v1.file_size != BE64(-1)) || (version == 2 && result_v2.file_size != BE64(-1))) { if(send(client->s, (char *)entry->d_name, d_name_len, 0) != d_name_len) { DPRINTF("send file name error on read dir entry (%d)\n", get_network_error()); return -1; } } return 0; }
static int process_open_cmd(client_t *client, netiso_open_cmd *cmd) { file_stat_t st; netiso_open_result result; char *filepath; uint16_t fp_len; int ret, viso = VISO_NONE; result.file_size = BE64(-1); result.mtime = BE64(0); fp_len = BE16(cmd->fp_len); //DPRINTF("fp_len = %d\n", fp_len); filepath = (char *)malloc(fp_len+1); if(!filepath) { DPRINTF("CRITICAL: memory allocation error\n"); return -1; } if(client->ro_file) { delete client->ro_file; } ret = recv_all(client->s, (void *)filepath, fp_len); filepath[fp_len] = 0; if(!strcmp(filepath, "/CLOSEFILE")) { free(filepath); return 0; } if(ret != fp_len) { DPRINTF("recv failed, getting filename for open: %d %d\n", ret, get_network_error()); free(filepath); return -1; } filepath = translate_path(filepath, 1, &viso); if(!filepath) { DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n"); return -1; } if(viso == VISO_NONE) { client->ro_file = new File(); } else { printf("building virtual iso...\n"); client->ro_file = new VIsoFile((viso == VISO_PS3)); } client->CD_SECTOR_SIZE = 2352; if(client->ro_file->open(filepath, O_RDONLY) < 0) { printf("open error on \"%s\" (viso=%d)\n", filepath + root_len, viso); delete client->ro_file; client->ro_file = NULL; } else { if(client->ro_file->fstat(&st) < 0) { DPRINTF("Error in fstat\n"); } else { result.file_size = BE64(st.file_size); result.mtime = BE64(st.mtime); if(viso != VISO_NONE || BE64(st.file_size) > 0x400000UL) printf("open %s\n", filepath + root_len); // detect cd sector size (2MB - 848MB) if(IS_RANGE(st.file_size, 0x200000UL, 0x35000000UL)) { char buffer[0x10] = ""; client->CD_SECTOR_SIZE = 0; client->ro_file->seek(0x8020UL, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC) == 0) client->CD_SECTOR_SIZE = 2048; else { client->ro_file->seek(0x9220UL, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC) == 0) client->CD_SECTOR_SIZE = 2336; else { client->ro_file->seek(0x9320UL, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC) == 0) client->CD_SECTOR_SIZE = 2352; else { client->ro_file->seek(0x9920UL, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC) == 0) client->CD_SECTOR_SIZE = 2448; }}} if(client->CD_SECTOR_SIZE > 0) printf("CD sector size: %i\n", client->CD_SECTOR_SIZE); else client->CD_SECTOR_SIZE = 2352; } } } #ifdef WIN32 DPRINTF("File size: %I64x\n", st.file_size); #else DPRINTF("File size: %llx\n", (long long unsigned int)st.file_size); #endif free(filepath); ret = send(client->s, (char *)&result, sizeof(result), 0); if(ret != sizeof(result)) { DPRINTF("open, send result error: %d %d\n", ret, get_network_error()); return -1; } return 0; }
static int64_t calculate_directory_size(char *path) { int64_t result = 0; DIR *d; struct dirent *entry; char *newpath; //DPRINTF("Calculate %s\n", path); file_stat_t st; if(stat_file(path, &st) < 0) return -1; d = opendir(path); if(!d) return -1; size_t d_name_len, path_len; path_len = strlen(path); while ((entry = readdir(d))) { if(IS_PARENT_DIR(entry->d_name)) continue; d_name_len = strlen(entry->d_name); if(IS_RANGE(d_name_len, 1, 65535)) { //DPRINTF("name: %s\n", entry->d_name); newpath = (char *)malloc(path_len + d_name_len + 2); if(!newpath) break; sprintf(newpath, "%s/%s", path, entry->d_name); if(stat_file(newpath, &st) < 0) { DPRINTF("calculate_directory_size: stat failed on %s\n", newpath); result = -1; break; } if((st.mode & S_IFDIR) == S_IFDIR) { int64_t temp = calculate_directory_size(newpath); if(temp < 0) { result = temp; break; } result += temp; } else if((st.mode & S_IFREG) == S_IFREG) { result += st.file_size; } free(newpath); } } if(newpath) free(newpath); closedir(d); return result; }
static int process_read_dir_cmd(client_t *client, netiso_read_dir_entry_cmd *cmd) { (void) cmd; int64_t dir_size = 0; netiso_read_dir_result result; memset(&result, 0, sizeof(result)); netiso_read_dir_result_data *dir_entries = (netiso_read_dir_result_data *) malloc(sizeof(netiso_read_dir_result_data) * MAX_ENTRIES); memset(dir_entries, 0, sizeof(netiso_read_dir_result_data) * MAX_ENTRIES); if(!client->dir || !client->dirpath || !dir_entries) { result.dir_size = (0); goto send_result_read_dir_cmd; } size_t d_name_len, dirpath_len; dirpath_len = strlen(client->dirpath); file_stat_t st; struct dirent *entry; while ((entry = readdir(client->dir))) { if(!entry) break; if(IS_PARENT_DIR(entry->d_name)) continue; d_name_len = strlen(entry->d_name); if(IS_RANGE(d_name_len, 1, MAX_PATH_LEN)) { char *path = (char*)malloc(dirpath_len + d_name_len + 2); if(!path) break; sprintf(path, "%s/%s", client->dirpath, entry->d_name); st.file_size = 0; st.mode = S_IFDIR; st.mtime = 0; st.atime = 0; st.ctime = 0; stat_file(path, &st); if(!st.mtime) st.mtime = st.ctime; if(!st.mtime) st.mtime = st.atime; if((st.mode & S_IFDIR) == S_IFDIR) { dir_entries[dir_size].file_size = (0); dir_entries[dir_size].is_directory = 1; } else { dir_entries[dir_size].file_size = BE64(st.file_size); dir_entries[dir_size].is_directory = 0; } snprintf(dir_entries[dir_size].name, MAX_PATH_LEN, "%s", entry->d_name); dir_entries[dir_size].mtime = BE64(st.mtime); free(path); dir_size++; if(dir_size >= MAX_ENTRIES) break; } } #ifdef WIN32 #ifdef MERGE_DRIVES if(root_len > 2 && dirpath_len > (root_len + 1) && strncmp(client->dirpath, root_directory, root_len) == 0) { memmove(client->dirpath + 2, client->dirpath + root_len, strlen(client->dirpath + root_len) + 1); client->dirpath[1] = ':'; dirpath_len = strlen(client->dirpath); if(client->dir) {closedir(client->dir); client->dir = NULL;} for(int drive = 'C'; drive <= 'Z'; drive++) { if(dir_size >= MAX_ENTRIES) break; if(ignore_drives) { bool ignore = false; for(uint8_t d = 0; d < ignore_drives_len; d++) if((ignore_drives[d] & 0xFF) == drive) {ignore = true; break;} if(ignore) continue; } client->dirpath[0] = drive; if(stat_file(client->dirpath, &st) < 0) continue; client->dir = opendir(client->dirpath); while ((entry = readdir(client->dir))) { if(!entry) break; if(IS_PARENT_DIR(entry->d_name)) continue; d_name_len = entry->d_namlen; //strlen(entry->d_name); if(IS_RANGE(d_name_len, 1, MAX_PATH_LEN)) { char *path = (char*)malloc(dirpath_len + d_name_len + 2); if(path) break; sprintf(path, "%s/%s", client->dirpath, entry->d_name); st.file_size = 0; st.mode = S_IFDIR; st.mtime = 0; st.atime = 0; st.ctime = 0; stat_file(path, &st); if(!st.mtime) st.mtime = st.ctime; if(!st.mtime) st.mtime = st.atime; if((st.mode & S_IFDIR) == S_IFDIR) { dir_entries[dir_size].file_size = (0); dir_entries[dir_size].is_directory = 1; } else { dir_entries[dir_size].file_size = BE64(st.file_size); dir_entries[dir_size].is_directory = 0; } snprintf(dir_entries[dir_size].name, MAX_PATH_LEN, "%s", entry->d_name); dir_entries[dir_size].mtime = BE64(st.mtime); free(path); dir_size++; if(dir_size >= MAX_ENTRIES) break; } } } } #endif #endif if(client->dir) {closedir(client->dir); client->dir = NULL;} send_result_read_dir_cmd: result.dir_size = BE64(dir_size); if(send(client->s, (const char*)&result, sizeof(result), 0) != sizeof(result)) { if(dir_entries) free(dir_entries); return -1; } if(dir_size > 0) { if(send(client->s, (const char*)dir_entries, (sizeof(netiso_read_dir_result_data)*dir_size), 0) != (int)(sizeof(netiso_read_dir_result_data)*dir_size)) { if(dir_entries) free(dir_entries); return -1; } } if(dir_entries) free(dir_entries); return 0; }
int main(int argc, char* argv[]) { int result; int nlx = NLx, nly = NLy, nlz = NLz; int nk = NK, nb = NB, nt = Nt; while((result = getopt(argc, argv, "l:k:b:s:hp")) != -1) { switch(result) { case 'l': { int x_, y_, z_; sscanf(optarg, "%dx%dx%d", &x_, &y_, &z_); if(!(IS_RANGE(4, x_, 512) && IS_RANGE(4, y_, 512) && IS_RANGE(4, z_, 512))) { fprintf(stderr, "lattice size is out of range. (4 <= NLx,y,z <= 512)\n"); exit(-1); } nlx = x_; nly = y_; nlz = z_; } break; case 'k': { int k_; sscanf(optarg, "%d", &k_); if(!IS_RANGE(1, k_, (int)pow(2,24))) { fprintf(stderr, "k-point size is out of range. (1 <= NK <= 2^24)\n"); exit(-1); } nk = k_; } break; case 'b': { int b_; sscanf(optarg, "%d", &b_); if(!IS_RANGE(1, b_, (int)pow(2,24))) { fprintf(stderr, "k-point size is out of range. (1 <= NK <= 2^24)\n"); exit(-1); } nb = b_; } break; case 's': { int t_; sscanf(optarg, "%d", &t_); if(t_ < 1) { fprintf(stderr, "Nt is invalid value (Nt < 1).\n"); exit(-1); } nt = t_; } break; case 'h': usage(argv[0]); exit(0); case 'p': print_optimize_message_(); exit(0); case '?': default: usage(argv[0]); exit(-1); } } stencil_perf_check_(&nlx, &nly, &nlz, &nk, &nb, &nt); return 0; }