All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Vladimir 'φ-coder/phcoder' Serbinenko" <phcoder@gmail.com>
To: grub-devel@gnu.org
Subject: Re: [PATCH] Add testpci command
Date: Fri, 28 Sep 2012 18:34:05 +0200	[thread overview]
Message-ID: <5065D17D.9000502@gmail.com> (raw)
In-Reply-To: <20120927214211.GS2289@earth.li>

[-- Attachment #1: Type: text/plain, Size: 3642 bytes --]

On 27.09.2012 23:42, Jonathan McDowell wrote:

> I have a machine with both Linux and Windows installed on the hard
> drive. Linux runs on the bare metal and I occasionally run the Windows
> install in a VM using KVM pointed at /dev/sda. However if I'm not quick
> enough, or Windows decides to reboot when I'm not around to notice, the
> grub running under KVM will decide to boot Linux and much confusion
> ensues.
> 
> I couldn't find an easy way to test what the running environment was, so
> I knocked up a simple testpci command that allows me to test for the
> existence of a PCI device and change the default boot option based on

This problem may be better solved by smbios tables or specific checker
for qemu since this same ID is actually a real chipset AFAIR.

> that. I have:
> 
> | if testpci 8086:1237; then
> |   set default="2"
> | fi

I'd prefer the syntax proposed by Andrey.

> === added file 'grub-core/commands/testpci.c'
> --- grub-core/commands/testpci.c	1970-01-01 00:00:00 +0000
> +++ grub-core/commands/testpci.c	2012-09-27 21:15:02 +0000
> @@ -0,0 +1,75 @@
> +/* testpci.c - Test for PCI device existence.  */
> +/*
> + *  GRUB  --  GRand Unified Bootloader
> + *  Copyright (C) 2012  Free Software Foundation, Inc.
> + *
> + *  GRUB 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 3 of the License, or
> + *  (at your option) any later version.
> + *
> + *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <grub/pci.h>
> +#include <grub/dl.h>
> +#include <grub/extcmd.h>
> +#include <grub/misc.h>
> +#include <grub/i18n.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +static int pcifound;
> +static grub_pci_id_t pcisearch;
> +
> +static int NESTED_FUNC_ATTR
> +grub_testpci_iter (grub_pci_device_t dev __attribute__ ((unused)),
> +                   grub_pci_id_t pciid)
> +{
> +  if (pciid == pcisearch) {
> +    pcifound = 1;
> +    return 1;
> +  }
> +
> +  return 0;
> +}
> +
> +static grub_err_t
> +grub_cmd_testpci (grub_command_t ctxt __attribute__ ((unused)),
> +		  int argc,
> +		  char **args)
> +{
> +  pcifound = 0;
> +
> +  if (argc != 1)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Device to search for required");

Please use the common message "obe argument expected" and add N_(...) so
it gets translated

> +
> +  if ((grub_strlen(args[0]) != 9) || args[0][4] != ':')
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Device ID must be xxxx:yyyy");
> +
> +  pcisearch = grub_strtoul(args[0], 0, 16) +
> +    (grub_strtoul(&args[0][5], 0, 16) << 16);
> +

This is slightly wrong. But I skip this part since with the other
parameter proposition this code part will disappear anyway.

> +  grub_pci_iterate (grub_testpci_iter);
> +
> +  return pcifound ? GRUB_ERR_NONE : grub_error (GRUB_ERR_TEST_FAILURE, "false");
> +}
> +
> +static grub_command_t cmd;
> +
> +GRUB_MOD_INIT(testpci)
> +{
> +  cmd = grub_register_command ("testpci", grub_cmd_testpci, "xxxx:yyyy",

It should be N_("VENDOR:DEVICE"), not undescriptive xxxx
-- 
Regards
Vladimir 'φ-coder/phcoder' Serbinenko


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 294 bytes --]

  parent reply	other threads:[~2012-09-28 16:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-27 21:42 [PATCH] Add testpci command Jonathan McDowell
2012-09-28  1:14 ` Seth Goldberg
2012-09-28  1:16   ` Seth Goldberg
2012-09-28 12:10 ` Andrey Borzenkov
2012-09-28 16:05   ` Vladimir 'φ-coder/phcoder' Serbinenko
2012-09-28 16:34 ` Vladimir 'φ-coder/phcoder' Serbinenko [this message]
2012-10-11  0:21 ` [PATCH] Add testpci command (v2) Jonathan McDowell
2012-10-11  2:54   ` Andrey Borzenkov
2012-10-12 22:18   ` [PATCH] Add testpci command (v3) Jonathan McDowell
2013-01-20 22:27     ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-01-25  5:12       ` [PATCH] Add testpci command (v4) Jonathan McDowell
2013-01-25  5:22         ` Andrey Borzenkov
2013-01-25  8:42           ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-01-25 10:06             ` Andrey Borzenkov
2013-01-28  6:47         ` [PATCH] Add testpci command (v5) Jonathan McDowell
2013-04-05  8:26         ` [PATCH] Add testpci command (v4) Vladimir 'φ-coder/phcoder' Serbinenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5065D17D.9000502@gmail.com \
    --to=phcoder@gmail.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.