示例#1
0
/* partition table display */
static void do_table(char *part)
{
    int fd, i;
    struct partition pt [PART_MAX];
    unsigned int second, base;
    unsigned char boot_sig[2];
    
    if (!strncmp(part, "/dev/", 5)) {
	if (strncmp(part+5, "md", 2) && isdigit(part[strlen(part)-1]) )
	    die("Not a device: '%s'", part);
	fd = open(part, O_RDONLY);
    } else
	fd = -1;
    if (fd<0) die("Unable to open '%s'", part);
    if (lseek(fd, PART_TABLE_OFFSET, SEEK_SET)<0) die("lseek failed");
    if (read(fd, pt, sizeof(pt)) != sizeof(pt)) die("read pt failed");
    if ( read(fd, &boot_sig[0], sizeof(boot_sig)) != sizeof(boot_sig)  ||
	boot_sig[0] != BOOT_SIGNATURE0 || boot_sig[1] != BOOT_SIGNATURE1 )
	die("read boot signature failed");
    {
	if (lseek(fd, MAX_BOOT_SIZE+2, SEEK_SET)<0) die("lseek s/n failed");
	if (read(fd, &second, sizeof(second)) != sizeof(second))
	    die("read s/n failed");
	printf(" S/N = %08X\n", second);
    }
    printf("%s\n", phead);
    second=base=0;
    for (i=0; i<PART_MAX; i++) {
	print_pt(i+1, pt[i]);
	if (is_extd_part(pt[i].sys_ind)) {
	    if (!base) base = pt[i].start_sect;
	    else die("invalid partition table: second extended partition found");
	}
    }
    i=5;
    while (verbose>0 && base) {
        if (llseek(fd, SECTORSIZE*(base+second) + PART_TABLE_OFFSET, SEEK_SET) < 0)
            die("secondary llseek failed");
	if (read(fd, pt, sizeof(pt)) != sizeof(pt)) die("secondary read pt failed");
	if ( read(fd, &boot_sig[0], sizeof(boot_sig)) != sizeof(boot_sig)  ||
	    boot_sig[0] != BOOT_SIGNATURE0 || boot_sig[1] != BOOT_SIGNATURE1 )
	    die("read second boot signature failed");
        print_pt(i++, pt[0]);
        if (is_extd_part(pt[1].sys_ind)) second=pt[1].start_sect;
        else base = 0;
    }
        
    close(fd);
}
示例#2
0
文件: lwvmedit.c 项目: dayt0n/relwvm
int edit_pt(struct _LwVM *LwVM, bool pt_no_crc)
{
	char input[CLI_INPUT_BUFF_SZ];
	while(1)
	{
		printf("Command (? for help): ");
		
		fgets(&input[0], CLI_INPUT_BUFF_SZ, stdin);
		
		if (!strcmp(input, "?\n") || !strcmp(input, "help\n") || !strcmp(input, "h\n"))
		{
			edit_help();
		}
		else if (!strcmp(input, "print\n") || !strcmp(input, "p\n"))
		{
			print_pt(LwVM, pt_no_crc);
		}
		else if (!strcmp(input, "quit\n") || !strcmp(input, "q\n"))
		{
			break;
		}
		else if (!strcmp(input, "write\n") || !strcmp(input, "w\n"))
		{
			damage_warning();
			return SAVE_CHANGES;
		}
		else if (!strcmp(input, "add\n") || !strcmp(input, "a\n"))
		{
			if (*(&LwVM->numPartitions) < 12)
			{
				memset(&LwVM->partitions[*(&LwVM->numPartitions)], 0, sizeof(LwVMPartitionRecord));
				
				/*uint64_t new_guid[2];
				FILE *random_f = fopen("/dev/random", "r");
				while(fread(&new_guid, 1, 16, random_f) != 16);
				fclose(random_f);*/
				
				void *new_guid = gen_guid();
				
				memcpy(&LwVM->partitions[*(&LwVM->numPartitions)].guid[0], &new_guid[0], 16);
				*(&LwVM->numPartitions) = *(&LwVM->numPartitions) + 1;
				free(new_guid);
				printf("Done.\n");
			}
			else printf("Can't create more than 12 partitions.\n");
		}
		else if (!strcmp(input, "rm\n") || !strcmp(input, "r\n"))
		{
			if (*(&LwVM->numPartitions) == 0)
			{
				printf("Nothing to delete.\n\n");
				continue;
			}
			
			printf("Type the number of the partition you want to remove [1-%u]: ", *(&LwVM->numPartitions));
			
			fgets(&input[0], CLI_INPUT_BUFF_SZ, stdin);
			
			int part_num = atoi(input);
			
			if (pt_splice(LwVM, part_num) == E_NOPART)
			{
				printf("No such partition.\n");
			}
			else
			{
				printf("Done.\n");
			}
		}
		else if (!strcmp(input, "edit\n") || !strcmp(input, "e\n"))
		{
			printf("Type the number of the partition you want to edit [1-%u]: ", *(&LwVM->numPartitions));
			fgets(&input[0], CLI_INPUT_BUFF_SZ, stdin);
			
			int part_to_edit = atoi(input);
			part_to_edit--;
			if (part_to_edit < 12 && part_to_edit >= 0 && part_to_edit < *(&LwVM->numPartitions))
			{
				printf("What parameter do you want to edit? [begin/end/type/name] ");
				fgets(&input[0], CLI_INPUT_BUFF_SZ, stdin);
				
				if (!strcmp(input, "begin\n"))
				{
					printf("Begin [0-%llu(MB/GB)]: ", *(&LwVM->mediaSize));
					
					uint64_t new_begin = get_param_input();
					if (new_begin > *(&LwVM->mediaSize))
					{
						printf("Can't create a partition beyond the disk.\n");
					}
					else
					{
						*(&LwVM->partitions[part_to_edit].begin) = new_begin;
					}
				}
				else if (!strcmp(input, "end\n"))
				{
					printf("End [0-%llu(MB/GB)]: ", *(&LwVM->mediaSize));
					
					uint64_t new_end = get_param_input();
					if (new_end > *(&LwVM->mediaSize))
					{
						printf("Can't set partition begin beyond the disk.\n");
					}
					else
					{
						*(&LwVM->partitions[part_to_edit].end) = new_end;
					}
				}
				else if (!strcmp(input, "type\n"))
				{
					while(1)
					{
						printf("Enter partition type you want (? for help): ");
						fgets(&input[0], CLI_INPUT_BUFF_SZ, stdin);
						
						if (!strcmp(input, "?\n"))
						{
							available_part_types();
						}
						else if (!strcmp(input, "HFS+\n") || !strcmp(input, "hfs+\n"))
						{
							memcpy(&LwVM->partitions[part_to_edit].type, LwVMPartitionTypeHFSPlus, sizeof(*LwVMPartitionTypeHFSPlus));
							break;
						}
						else if (!strcmp(input, "LinuxData\n") || !strcmp(input, "linuxdata\n"))
						{
							memcpy(&LwVM->partitions[part_to_edit].type, LwVMPartitionTypeLinuxData, sizeof(*LwVMPartitionTypeLinuxData));
							break;
						}
						else if (!strcmp(input, "disabled\n") || !strcmp(input, "Disabled\n"))
						{
							memcpy(&LwVM->partitions[part_to_edit].type, LwVMPartitionTypeDisabled, sizeof(*LwVMPartitionTypeDisabled));
							break;
						}
						else
						{
							printf("Unknown type: %s\n", input);
							available_part_types();
						}
					}
				}
				else if (!strcmp(input, "name\n"))
				{
					printf("Type new name of the partition (max of 36 characters): ");
					fgets(&input[0], CLI_INPUT_BUFF_SZ, stdin);
					
					input[strlen(input) - 1] = 0; //Remove the \n byte.
					char *lwvm_name = str_to_lwvm_name(input);
					memcpy(&LwVM->partitions[part_to_edit].partitionName, lwvm_name, 0x48);
					
					free(lwvm_name);
				}
				else
				{
					printf("Invalid parameter name.\n");
				}
			}
			else
			{
				printf("Invalid number.\n");
			}
		}
		else if (strcmp(input, " \n") && strcmp(input, "\n"))
		{
			edit_help();
		}
		
		printf("\n");
	}
	
	return DISCARD_CHANGES;
}
示例#3
0
文件: lwvmedit.c 项目: dayt0n/relwvm
int main(int argc, const char *argv[])
{
	if (argc < 2)
	{
		help(argv[0]);
		return 1;
	}
	
	int fn_arg = 0;
	
	const char *map_fn_arg = 0;
	
	int action = 0;
	
	int i;
	for (i = 1; i < argc; i++)
	{
		if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
		{
			help(argv[0]);
			return 0;
		}
		else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
		{
			version();
			return 0;
		}
		else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list"))
		{
			action = A_LIST;
		}
		else if (!strcmp(argv[i], "-E") || !strcmp(argv[i], "--ignore-errors"))
		{
			ignore_errors = true;
		}
		else if (!strcmp(argv[i], "-H") || !strcmp(argv[i], "--human-readable"))
		{
			human_readable = true;
		}
		else if (!strcmp(argv[i], "-e") || !strcmp(argv[i], "--edit"))
		{
			action = A_EDIT;
		}
		else if (!strcmp(argv[i], "-f"))
		{
			i++;
			map_fn_arg = argv[i];
		}
		else if (!strncmp(argv[i], "--from-file=", 12))
		{
			map_fn_arg = argv[i] + 12;
		}
		else if (argv[i][0] != '-')
		{
			fn_arg = i;
		}
		else
		{
			printf("Unknown argument: %s\n", argv[i]);
			help(argv[0]);
			return 1;
		}
	}
	
	if (action == 0 && map_fn_arg == 0)
	{
		help(argv[0]);
		return 1;
	}
	
	FILE *img_f = fopen(argv[fn_arg], "r+b");
	
	if (img_f == 0)
	{
		printf("Can't open file: %s\n", argv[fn_arg]);
		
		errno_print();
		
		return 1;
	}
	
	struct _LwVM *LwVM = malloc(sizeof(*LwVM));
	fseek(img_f, 0L, SEEK_SET);
	if (fread(LwVM, 1, sizeof(*LwVM), img_f) != 4096)
	{
		printf("Can't read file: %s\n", argv[fn_arg]);
		
		errno_print();
		
		cleanup(img_f, LwVM);
		
		return 1;
	}
	
	bool pt_no_crc = !memcmp(LwVM->type, LwVMType_noCRC, sizeof(*LwVMType_noCRC));
	if (memcmp(LwVM->type, LwVMType, sizeof(*LwVMType)) != 0 && !pt_no_crc)
	{
		printf("LwVM has unknown type or was damaged, ");
		if (ignore_errors) printf("continuing anyway...\n");
		else
		{
			printf("exiting...\n");
			cleanup(img_f, LwVM);
			return 1;
		}
	}
	
	if (map_fn_arg)
	{
		struct stat st;
		if (stat(map_fn_arg, &st) != 0)
		{
			printf("Can't stat file: %s\n", map_fn_arg);
			
			errno_print();
			
			cleanup(img_f, LwVM);
			return 1;
		}
		
		FILE *map_f = fopen(map_fn_arg, "r");
		if (map_f == 0)
		{
			printf("Can't open map: %s\n", map_fn_arg);
			
			errno_print();
			
			cleanup(img_f, LwVM);
			return 1;
		}
		
		char *map = malloc(st.st_size);
		if (fread(map, 1, st.st_size, map_f) != st.st_size)
		{
			printf("Can't read map: %s\n", map_fn_arg);
			
			errno_print();
			
			cleanup(img_f, LwVM);
			fclose(map_f);
			return 1;
		}
		
		fclose(map_f);
		
		int map_load_status = load_map(LwVM, map);
		
		if (map_load_status)
		{
			printf("Map parse failed.\n");
			cleanup(img_f, LwVM);
			return 1;
		}
		
		printf("Map load success.\n");
		write_lwvm(img_f, LwVM);
		cleanup(img_f, LwVM);
		return 0;
	}
	
	if (action == A_LIST)
	{
		print_pt(LwVM, pt_no_crc);
	}
	else if (action == A_EDIT)
	{
		int status = edit_pt(LwVM, pt_no_crc);
		if (status == SAVE_CHANGES)
		{
			return write_lwvm(img_f, LwVM);
		}
	}
	
	cleanup(img_f, LwVM);
	
	
	return 0;
}
示例#4
0
文件: gen_foot.c 项目: nllpntr/l-echo
int main()
{
	PRINT_STATS;
	printf("void draw_foot()\n{\n");
	printf("#ifndef ECHO_NDS\n");
	float theta = 0;
	while(theta <= PI)
	{
		float phi = TOP, u = 0;
		printf("\tglBegin(GL_QUAD_STRIP);\n");
		while(phi <= BOTTOM)
		{
			print_sphere_pt(theta, phi);
			print_sphere_pt(theta + ANGLE_INCR, phi);
			phi += ANGLE_INCR;
		}
		printf("\tglEnd();\n");
		theta += ANGLE_INCR;
	}
	printf("#else\n");
	printf("\tglBegin(GL_QUAD_STRIP);\n");	//12 verts
		print_pt(-X_RADIUS, 0, 0);
		print_pt(-X_RADIUS, 0, Z_RADIUS);
		print_pt(-X_RADIUS, Y_RADIUS, 0);
		print_pt(-X_RADIUS, Y_RADIUS, Z_RADIUS);
		print_pt(X_RADIUS, Y_RADIUS, 0);
		print_pt(X_RADIUS, Y_RADIUS, Z_RADIUS);
		print_pt(X_RADIUS, 0, 0);
		print_pt(X_RADIUS, 0, Z_RADIUS);
	printf("\tglEnd();\n");
	printf("\tglBegin(GL_QUADS);\n");
		print_pt(-X_RADIUS, 0, Z_RADIUS);
		print_pt(-X_RADIUS, Y_RADIUS, Z_RADIUS);
		print_pt(X_RADIUS, Y_RADIUS, Z_RADIUS);
		print_pt(X_RADIUS, 0, Z_RADIUS);
	printf("\tglEnd();\n");
	printf("#endif\n");
	printf("}\n");
}