Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  fs/partitions/mac.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Code extracted from drivers/block/genhd.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Copyright (C) 1991-1998  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Re-organised Feb 1998 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "check.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "mac.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #ifdef CONFIG_PPC_PMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/machdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) extern void note_bootable_part(dev_t dev, int part, int goodness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * Code to understand MacOS partition tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static inline void mac_fix_string(char *stg, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	for (i = len - 1; i >= 0 && stg[i] == ' '; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		stg[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) int mac_partition(struct parsed_partitions *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	Sector sect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	unsigned char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int slot, blocks_in_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	unsigned secsize, datasize, partoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #ifdef CONFIG_PPC_PMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int found_root = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int found_root_goodness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct mac_partition *part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct mac_driver_desc *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	/* Get 0th block and look at the first partition map entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	md = read_part_sector(state, 0, &sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (!md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		put_dev_sector(sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	secsize = be16_to_cpu(md->block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	put_dev_sector(sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	datasize = round_down(secsize, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	data = read_part_sector(state, datasize / 512, &sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	partoffset = secsize % 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (partoffset + sizeof(*part) > datasize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	part = (struct mac_partition *) (data + partoffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		put_dev_sector(sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return 0;		/* not a MacOS disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	blocks_in_map = be32_to_cpu(part->map_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (blocks_in_map < 0 || blocks_in_map >= DISK_MAX_PARTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		put_dev_sector(sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (blocks_in_map >= state->limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		blocks_in_map = state->limit - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	strlcat(state->pp_buf, " [mac]", PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	for (slot = 1; slot <= blocks_in_map; ++slot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		int pos = slot * secsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		put_dev_sector(sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		data = read_part_sector(state, pos/512, &sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		part = (struct mac_partition *) (data + pos%512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		put_partition(state, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			be32_to_cpu(part->start_block) * (secsize/512),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			be32_to_cpu(part->block_count) * (secsize/512));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (!strncasecmp(part->type, "Linux_RAID", 10))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			state->parts[slot].flags = ADDPART_FLAG_RAID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #ifdef CONFIG_PPC_PMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		 * If this is the first bootable partition, tell the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		 * setup code, in case it wants to make this the root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		if (machine_is(powermac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			int goodness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			mac_fix_string(part->processor, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			mac_fix_string(part->name, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			mac_fix_string(part->type, 32);					
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		    
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			if ((be32_to_cpu(part->status) & MAC_STATUS_BOOTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			    && strcasecmp(part->processor, "powerpc") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				goodness++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			if (strcasecmp(part->type, "Apple_UNIX_SVR2") == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			    || (strncasecmp(part->type, "Linux", 5) == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			        && strcasecmp(part->type, "Linux_swap") != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				int i, l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 				goodness++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				l = strlen(part->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				if (strcmp(part->name, "/") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 					goodness++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				for (i = 0; i <= l - 4; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 					if (strncasecmp(part->name + i, "root",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 						     4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 						goodness += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 						break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 				if (strncasecmp(part->name, "swap", 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 					goodness--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			if (goodness > found_root_goodness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				found_root = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				found_root_goodness = goodness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #endif /* CONFIG_PPC_PMAC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #ifdef CONFIG_PPC_PMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (found_root_goodness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		note_bootable_part(state->bdev->bd_dev, found_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				   found_root_goodness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	put_dev_sector(sect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	strlcat(state->pp_buf, "\n", PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }