1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  
     | 
    
      /*
 *  qemubuilder: pbuilder with qemu
 *  arch-specific code.
 *
 *  Copyright (C) 2009 Junichi Uekawa
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <stdarg.h>
#include <assert.h>
#include <termios.h>
#include <time.h>
#include <locale.h>
#include "parameter.h"
#include "qemuarch.h"
#include "file.h"
/**
 * arch-specific routine; disk device name to use
 */
const char *qemu_arch_diskdevice(const struct pbuilderconfig *pc) {
	if (pc->arch_diskdevice) {
		return pc->arch_diskdevice;
	}
	return "sd";
}
/**
 * arch-specific routine; the console device to make
 */
const int qemu_create_arch_serialdevice(const char *basedir, const char *arch) {
	dev_t consoledev;
	if (!strcmp(arch, "arm") || !strcmp(arch, "armel") ||
		!strcmp(arch, "armhf") || !strcmp(arch, "arm64")) {
		consoledev = makedev(204, 64);
	} else {
		consoledev = makedev(4, 64);
	}
	return mknod_inside_chroot(
		basedir, "dev/console", S_IFCHR | 0660, consoledev);
}
/**
 * arch-specific routine; make device files inside chroot
 */
const int qemu_create_arch_devices(const char *basedir, const char *arch) {
	int ret = 0;
	char *s = 0;
	asprintf(&s, "%s/%s", basedir, "dev");
	if (-1 == mkdir(s, 0777)) {
		log_perror("mkdir chroot-/dev");
		ret = 1;
	}
	free(s);
	ret += qemu_create_arch_serialdevice(basedir, arch);
	ret += mknod_inside_chroot(
		basedir, "dev/ttyS0", S_IFCHR | 0660, makedev(4, 64));
	ret += mknod_inside_chroot(
		basedir, "dev/ttyAMA0", S_IFCHR | 0660, makedev(204, 64));
	ret +=
		mknod_inside_chroot(basedir, "dev/sda", S_IFBLK | 0660, makedev(8, 0));
	ret +=
		mknod_inside_chroot(basedir, "dev/sdb", S_IFBLK | 0660, makedev(8, 16));
	ret +=
		mknod_inside_chroot(basedir, "dev/hda", S_IFBLK | 0660, makedev(3, 0));
	ret +=
		mknod_inside_chroot(basedir, "dev/hdb", S_IFBLK | 0660, makedev(3, 64));
	return ret;
}
/**
 * get output of dpkg --print-architecture
 * returns a malloc'd string, you need to free it.
 */
char *get_host_dpkg_arch() {
	FILE *f = popen("dpkg --print-architecture", "r");
	char *host_arch;
	fscanf(f, "%ms", &host_arch);
	pclose(f);
	return host_arch;
}
/**
 * arch-specific routine; qemu command to use.
 */
const char *qemu_arch_qemu(const char *arch) {
	if (!strcmp(arch, "arm") || !strcmp(arch, "armel") ||
		!strcmp(arch, "armhf")) {
		return "qemu-system-arm";
	} else if (!strcmp(arch, "arm64")) {
		return "qemu-system-aarch64";
	} else if (!strcmp(arch, "mips")) {
		return "qemu-system-mips";
	} else if (!strcmp(arch, "mipsel")) {
		return "qemu-system-mipsel";
	} else if (!strcmp(arch, "i386") || !strcmp(arch, "hurd-i386") ||
			   !strcmp(arch, "amd64") || !strcmp(arch, "x32")) {
		return "qemu-system-x86_64";
	} else if (!strcmp(arch, "hurd-i386")) {
		return "qemu";
	} else if (!strcmp(arch, "powerpc")) {
		return "qemu-system-ppc";
	} else if (!strcmp(arch, "sparc")) {
		return "qemu-system-sparc";
	} else {
		return NULL;
	}
}
/**
 * arch-specific routine; the machine spec for this arch
 */
const char *qemu_arch_qemumachine(const char *arch) {
	if (!strcmp(arch, "arm") || !strcmp(arch, "armel")) {
		return "versatilepb";
	} else if (!strcmp(arch, "armhf") || !strcmp(arch, "arm64")) {
		return "virt";
	} else if (!strcmp(arch, "i386") || !strcmp(arch, "hurd-i386") ||
			   !strcmp(arch, "amd64") || !strcmp(arch, "x32")) {
		return "pc";
	} else if (!strcmp(arch, "mips") || !strcmp(arch, "mipsel")) {
		return "malta";
	} else if (!strcmp(arch, "powerpc")) {
		return "prep";
	} else if (!strcmp(arch, "sparc")) {
		return "SS-5";
	}
	return NULL;
}
/**
 * arch-specific routine; the serial device
 */
const char *qemu_arch_tty(const char *arch) {
	if (!strcmp(arch, "arm") || !strcmp(arch, "armel") ||
		!strcmp(arch, "armhf") || !strcmp(arch, "arm64")) {
		return "ttyAMA0,115200n8";
	}
	return "ttyS0,115200n8";
}
/**
 * arch-specific routine; the CPU
 */
const char *qemu_arch_cpu(const char *arch) {
	if (!strcmp(arch, "arm64")) {
		return "cortex-a57";
	}
	return NULL;
}
 
     |